]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
Add date-related info insets
[lyx.git] / src / insets / InsetInfo.cpp
1 /**
2  * \file InsetInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 #include <config.h>
12
13 #include "InsetInfo.h"
14 #include "LyX.h"
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
19 #include "Font.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "InsetGraphics.h"
23 #include "InsetSpecialChar.h"
24 #include "KeyMap.h"
25 #include "LaTeXFeatures.h"
26 #include "Language.h"
27 #include "LayoutFile.h"
28 #include "Length.h"
29 #include "LyXAction.h"
30 #include "LyXRC.h"
31 #include "LyXVC.h"
32 #include "Lexer.h"
33 #include "Paragraph.h"
34 #include "ParIterator.h"
35 #include "ParagraphParameters.h"
36 #include "version.h"
37
38 #include "frontends/Application.h"
39
40 #include "support/convert.h"
41 #include "support/debug.h"
42 #include "support/docstream.h"
43 #include "support/docstring_list.h"
44 #include "support/ExceptionMessage.h"
45 #include "support/FileName.h"
46 #include "support/filetools.h"
47 #include "support/gettext.h"
48 #include "support/Messages.h"
49 #include "support/lstrings.h"
50 #include "support/qstring_helpers.h"
51 #include "support/Translator.h"
52
53 #include <sstream>
54
55 #include <QtGui/QImage>
56 #include <QDate>
57 #include <QLocale>
58
59 using namespace std;
60 using namespace lyx::support;
61
62 namespace lyx {
63
64 namespace {
65
66 typedef Translator<InsetInfo::info_type, string> NameTranslator;
67
68 NameTranslator const initTranslator()
69 {
70         NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
71
72         translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
73         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
74         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
75         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
76         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
77         translator.addPair(InsetInfo::MENU_INFO, "menu");
78         translator.addPair(InsetInfo::ICON_INFO, "icon");
79         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
80         translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
81         translator.addPair(InsetInfo::VCS_INFO, "vcs");
82         translator.addPair(InsetInfo::DATE_INFO, "date");
83         translator.addPair(InsetInfo::MODDATE_INFO, "moddate");
84         translator.addPair(InsetInfo::FIXDATE_INFO, "fixdate");
85
86         return translator;
87 }
88
89 /// The translator between the information type enum and corresponding string.
90 NameTranslator const & nameTranslator()
91 {
92         static NameTranslator const translator = initTranslator();
93         return translator;
94 }
95
96
97 typedef Translator<InsetInfo::info_type, string> DefaultValueTranslator;
98
99 DefaultValueTranslator const initDVTranslator()
100 {
101         DefaultValueTranslator translator(InsetInfo::UNKNOWN_INFO, "");
102
103         translator.addPair(InsetInfo::SHORTCUTS_INFO, "info-insert");
104         translator.addPair(InsetInfo::SHORTCUT_INFO, "info-insert");
105         translator.addPair(InsetInfo::LYXRC_INFO, "user_name");
106         translator.addPair(InsetInfo::PACKAGE_INFO, "graphics");
107         translator.addPair(InsetInfo::TEXTCLASS_INFO, "article");
108         translator.addPair(InsetInfo::MENU_INFO, "info-insert");
109         translator.addPair(InsetInfo::ICON_INFO, "info-insert");
110         translator.addPair(InsetInfo::BUFFER_INFO, "name");
111         translator.addPair(InsetInfo::LYX_INFO, "version");
112         translator.addPair(InsetInfo::VCS_INFO, "revision");
113         translator.addPair(InsetInfo::DATE_INFO, "loclong");
114         translator.addPair(InsetInfo::MODDATE_INFO, "loclong");
115         translator.addPair(InsetInfo::FIXDATE_INFO, "loclong");
116
117         return translator;
118 }
119
120 /// The translator between the information type enum and some sensible default value.
121 DefaultValueTranslator const & defaultValueTranslator()
122 {
123         static DefaultValueTranslator const translator = initDVTranslator();
124         return translator;
125 }
126
127 } // namespace
128
129 /////////////////////////////////////////////////////////////////////////
130 //
131 // InsetInfo
132 //
133 /////////////////////////////////////////////////////////////////////////
134
135
136
137 InsetInfo::InsetInfo(Buffer * buf, string const & name)
138         : InsetCollapsible(buf), initialized_(false), 
139           type_(UNKNOWN_INFO), name_(), force_ltr_(false)
140 {
141         setInfo(name);
142         status_ = Collapsed;
143 }
144
145
146 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
147 {
148         // do not allow the cursor to be set in this Inset
149         return Inset::editXY(cur, x, y);
150 }
151
152
153 string InsetInfo::infoType() const
154 {
155         return nameTranslator().find(type_);
156 }
157
158
159 docstring InsetInfo::layoutName() const
160 {
161         return from_ascii("Info:" + infoType());
162 }
163
164
165 docstring InsetInfo::toolTip(BufferView const &, int, int) const
166 {
167         docstring result;
168         switch (nameTranslator().find(infoType())) {
169         case UNKNOWN_INFO:
170                 result = _("Invalid information inset");
171                 break;
172         case SHORTCUT_INFO:
173                 result = bformat(_("The keybard shortcut for the function '%1$s'"),
174                                 from_utf8(name_));
175                 break;
176         case SHORTCUTS_INFO:
177                 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
178                                 from_utf8(name_));
179                 break;
180         case MENU_INFO: 
181                 result = bformat(_("The menu location for the function '%1$s'"),
182                                 from_utf8(name_));
183                 break;
184         case ICON_INFO:
185                 result = bformat(_("The toolbar icon for the function '%1$s'"),
186                                 from_utf8(name_));
187                 break;
188         case LYXRC_INFO:
189                 result = bformat(_("The preference setting for the preference key '%1$s'"),
190                                 from_utf8(name_));
191                 break;
192         case PACKAGE_INFO:
193                 result = bformat(_("Availability of the LaTeX package '%1$s'"),
194                                 from_utf8(name_));
195                 break;
196         case TEXTCLASS_INFO:
197                 result = bformat(_("Availability of the LaTeX class '%1$s'"),
198                                 from_utf8(name_));
199                 break;
200         case BUFFER_INFO:
201                 if (name_ == "name")
202                         result = _("The name of this file");
203                 else if (name_ == "path")
204                         result = _("The path where this file is saved");
205                 else if (name_ == "class")
206                         result = _("The class this document uses");
207                 break;
208         case VCS_INFO:
209                 if (name_ == "revision")
210                         result = _("Version control revision");
211                 else if (name_ == "tree-revision")
212                         result = _("Version control tree revision");
213                 else if (name_ == "author")
214                          result = _("Version control author");
215                 else if (name_ == "date")
216                         result = _("Version control date");
217                 else if (name_ == "time")
218                         result = _("Version control time");
219                 break;
220         case LYX_INFO:
221                 result = _("The current LyX version");
222                 break;
223         case DATE_INFO:
224                 result = _("The current date");
225                 break;
226         case MODDATE_INFO:
227                 result = _("The date of last save");
228                 break;
229         case FIXDATE_INFO:
230                 result = _("A static date");
231                 break;
232         }
233
234         return result;
235 }
236
237
238 void InsetInfo::read(Lexer & lex)
239 {
240         string token;
241         while (lex.isOK()) {
242                 lex.next();
243                 token = lex.getString();
244                 if (token == "type") {
245                         lex.next();
246                         token = lex.getString();
247                         type_ = nameTranslator().find(token);
248                 } else if (token == "arg") {
249                         lex.next(true);
250                         name_ = lex.getString();
251                 } else if (token == "\\end_inset")
252                         break;
253         }
254         if (token != "\\end_inset") {
255                 lex.printError("Missing \\end_inset at this point");
256                 throw ExceptionMessage(WarningException,
257                         _("Missing \\end_inset at this point."),
258                         from_utf8(token));
259         }
260 }
261
262
263 void InsetInfo::write(ostream & os) const
264 {
265         os << "Info\ntype  \"" << infoType()
266            << "\"\narg   " << Lexer::quoteString(name_);
267 }
268
269
270 bool InsetInfo::validateModifyArgument(docstring const & arg) const
271 {
272         string type;
273         string name = trim(split(to_utf8(arg), type, ' '));
274
275         switch (nameTranslator().find(type)) {
276         case UNKNOWN_INFO:
277                 return false;
278
279         case SHORTCUT_INFO:
280         case SHORTCUTS_INFO:
281         case MENU_INFO: {
282                 FuncRequest func = lyxaction.lookupFunc(name);
283                 return func.action() != LFUN_UNKNOWN_ACTION;
284         }
285
286         case ICON_INFO: {
287                 FuncCode const action = lyxaction.lookupFunc(name).action();
288                 if (action == LFUN_UNKNOWN_ACTION) {
289                         string dir = "images";
290                         return !imageLibFileSearch(dir, name, "svgz,png").empty();
291                 }
292                 return true;
293         }
294
295         case LYXRC_INFO: {
296                 set<string> rcs = lyxrc.getRCs();
297                 return rcs.find(name) != rcs.end();
298         }
299
300         case PACKAGE_INFO:
301         case TEXTCLASS_INFO:
302                 return true;
303
304         case BUFFER_INFO:
305                 return (name == "name" || name == "path" || name == "class");
306
307         case VCS_INFO:
308                 if (name == "revision" || name == "tree-revision"
309                     || name == "author" || name == "date" || name == "time")
310                         return buffer().lyxvc().inUse();
311                 return false;
312
313         case LYX_INFO:
314                 return name == "version";
315
316         case FIXDATE_INFO: {
317                 string date;
318                 string piece;
319                 date = split(name, piece, '@');
320                 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
321                         return false;
322                 if (!piece.empty())
323                         name = piece;
324         }
325         // fall through
326         case DATE_INFO:
327         case MODDATE_INFO: {
328                 if (name == "long" || name == "short" || name == "ISO")
329                         return true;
330                 else {
331                         QDate date = QDate::currentDate();
332                         return !date.toString(toqstr(name)).isEmpty();
333                 }
334         }
335         }
336
337         return false;
338 }
339
340
341 namespace{
342 set<string> getTexFileList(string const & filename)
343 {
344         set<string> list;
345         FileName const file = libFileSearch(string(), filename);
346         if (file.empty())
347                 return list;
348
349         // FIXME Unicode.
350         vector<docstring> doclist =
351                 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
352
353         // Normalise paths like /foo//bar ==> /foo/bar
354         for (auto doc : doclist) {
355                 subst(doc, from_ascii("\r"), docstring());
356                 while (contains(doc, from_ascii("//")))
357                         subst(doc, from_ascii("//"), from_ascii("/"));
358                 if (!doc.empty())
359                         list.insert(removeExtension(onlyFileName(to_utf8(doc))));
360         }
361
362         // remove duplicates
363         return list;
364 }
365 } // namespace anon
366
367
368 docstring InsetInfo::getDate(string const name, QDate const date) const
369 {
370         QLocale loc;
371         if (lang_)
372                 loc = QLocale(toqstr(lang_->code()));
373         if (name == "long")
374                 return qstring_to_ucs4(loc.toString(date, QLocale::LongFormat));
375         else if (name == "short")
376                 return qstring_to_ucs4(loc.toString(date, QLocale::ShortFormat));
377         else if (name == "ISO")
378                 return qstring_to_ucs4(date.toString(Qt::ISODate));
379         else if (name == "loclong")
380                 return qstring_to_ucs4(loc.toString(date, toqstr(lang_->dateFormat(0))));
381         else if (name == "locmedium")
382                 return qstring_to_ucs4(loc.toString(date, toqstr(lang_->dateFormat(1))));
383         else if (name == "locshort")
384                 return qstring_to_ucs4(loc.toString(date, toqstr(lang_->dateFormat(2))));
385         else
386                 return qstring_to_ucs4(loc.toString(date, toqstr(name)));
387 }
388
389
390 vector<pair<string,docstring>> InsetInfo::getArguments(string const & type) const
391 {
392         vector<pair<string,docstring>> result;
393
394         switch (nameTranslator().find(type)) {
395         case UNKNOWN_INFO:
396                 result.push_back(make_pair("invalid", _("Please select a valid type!")));
397                 break;
398
399         case SHORTCUT_INFO:
400         case SHORTCUTS_INFO:
401         case MENU_INFO:
402         case ICON_INFO: {
403                 result.push_back(make_pair("custom", _("Custom")));
404                 LyXAction::const_iterator fit = lyxaction.func_begin();
405                 LyXAction::const_iterator const fen = lyxaction.func_end();
406                 for (; fit != fen; ++fit) {
407                         string const lfun = fit->first;
408                         if (!lfun.empty())
409                                 result.push_back(make_pair(lfun, from_ascii(lfun)));
410                 }
411                 break;
412         }
413
414         case LYXRC_INFO: {
415                 result.push_back(make_pair("custom", _("Custom")));
416                 set<string> rcs = lyxrc.getRCs();
417                 for (auto const & rc : rcs)
418                         result.push_back(make_pair(rc, from_ascii(rc)));
419                 break;
420         }
421
422         case PACKAGE_INFO:
423         case TEXTCLASS_INFO: {
424                 result.push_back(make_pair("custom", _("Custom")));
425                 string const filename = (type == "package") ? "styFiles.lst"
426                                                             : "clsFiles.lst";
427                 set<string> flist = getTexFileList(filename);
428                 for (auto const & f : flist)
429                         result.push_back(make_pair(f, from_utf8(f)));
430                 break;
431         }
432
433         case BUFFER_INFO:
434                 result.push_back(make_pair("name", _("File name")));
435                 result.push_back(make_pair("path", _("File path")));
436                 result.push_back(make_pair("class", _("Used text class")));
437                 break;
438
439         case VCS_INFO: {
440                 if (!buffer().lyxvc().inUse()) {
441                         result.push_back(make_pair("invalid", _("No version control!")));
442                         break;
443                 }
444                 result.push_back(make_pair("revision", _("Revision[[Version Control]]")));
445                 result.push_back(make_pair("tree-revision", _("Tree revision")));
446                 result.push_back(make_pair("author", _("Author")));
447                 result.push_back(make_pair("date", _("Date")));
448                 result.push_back(make_pair("time", _("Time")));
449                 break;
450         }
451
452         case LYX_INFO:
453                 result.push_back(make_pair("version", _("LyX version")));
454                 break;
455
456         case FIXDATE_INFO:
457         case DATE_INFO:
458         case MODDATE_INFO:
459                 string const dt = split(name_, '@');
460                 QDate date;
461                 if (type == "moddate")
462                         date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
463                 else if (type == "fixdate" && !dt.empty())
464                         date = QDate::fromString(toqstr(dt), Qt::ISODate);
465                 else
466                         date = QDate::currentDate();
467                 result.push_back(make_pair("long",getDate("long", date)));
468                 result.push_back(make_pair("short", getDate("short", date)));
469                 result.push_back(make_pair("loclong", getDate("loclong", date)));
470                 result.push_back(make_pair("locmedium", getDate("locmedium", date)));
471                 result.push_back(make_pair("locshort", getDate("locshort", date)));
472                 result.push_back(make_pair("ISO", getDate("ISO", date)));
473                 result.push_back(make_pair("yyyy", getDate("yyyy", date)));
474                 result.push_back(make_pair("MMMM", getDate("MMMM", date)));
475                 result.push_back(make_pair("MMM", getDate("MMM", date)));
476                 result.push_back(make_pair("dddd", getDate("dddd", date)));
477                 result.push_back(make_pair("ddd", getDate("ddd", date)));
478                 result.push_back(make_pair("custom", _("Custom")));
479                 break;
480         }
481
482         return result;
483 }
484
485
486 bool InsetInfo::showInsetDialog(BufferView * bv) const
487 {
488         bv->showDialog("info");
489         return true;
490 }
491
492
493 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
494                 FuncStatus & flag) const
495 {
496         switch (cmd.action()) {
497         case LFUN_INSET_SETTINGS:
498                 return InsetCollapsible::getStatus(cur, cmd, flag);
499
500         case LFUN_INSET_DIALOG_UPDATE:
501         case LFUN_INSET_COPY_AS:
502                 flag.setEnabled(true);
503                 return true;
504
505         case LFUN_INSET_MODIFY:
506                 if (validateModifyArgument(cmd.argument())) {
507                         flag.setEnabled(true);
508                         string typestr;
509                         string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
510                         info_type type = nameTranslator().find(typestr);
511                         string origname = name_;
512                         if (type == FIXDATE_INFO)
513                                 split(name_, origname, '@');
514                         flag.setOnOff(type == type_ && name == origname);
515                         return true;
516                 }
517                 //fall through
518
519         default:
520                 return false;
521         }
522 }
523
524
525 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
526 {
527         switch (cmd.action()) {
528         case LFUN_INSET_MODIFY:
529                 cur.recordUndo();
530                 setInfo(to_utf8(cmd.argument()));
531                 cur.forceBufferUpdate();
532                 initialized_ = false;
533                 break;
534
535         case LFUN_INSET_COPY_AS: {
536                 cap::clearSelection();
537                 Cursor copy(cur);
538                 copy.pushBackward(*this);
539                 copy.pit() = 0;
540                 copy.pos() = 0;
541                 copy.resetAnchor();
542                 copy.pit() = copy.lastpit();
543                 copy.pos() = copy.lastpos();
544                 copy.setSelection();
545                 cap::copySelection(copy);
546                 break;
547         }
548
549         default:
550                 InsetCollapsible::doDispatch(cur, cmd);
551                 break;
552         }
553 }
554
555
556 void InsetInfo::setInfo(string const & name)
557 {
558         if (name.empty())
559                 return;
560
561         string saved_date_specifier;
562         // Store old date specifier for potential re-use
563         if (!name_.empty())
564                 saved_date_specifier = split(name_, '@');
565         // info_type name
566         string type;
567         name_ = trim(split(name, type, ' '));
568         type_ = nameTranslator().find(type);
569         if (name_.empty())
570                 name_ = defaultValueTranslator().find(type_);
571         if (type_ == FIXDATE_INFO) {
572                 string const date_specifier = split(name_, '@');
573                 // If an explicit new fix date is specified, use that
574                 // Otherwise, use the old one or, if there is none,
575                 // the current date
576                 if (date_specifier.empty()) {
577                         if (saved_date_specifier.empty())
578                                 name_ += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
579                         else
580                                 name_ += "@" + saved_date_specifier;
581                 }
582         }
583 }
584
585
586 void InsetInfo::error(docstring const & err, Language const * lang)
587 {
588         setText(bformat(translateIfPossible(err, lang->code()), from_utf8(name_)),
589                 Font(inherit_font, lang), false);
590 }
591
592
593 void InsetInfo::info(docstring const & err, Language const * lang)
594 {
595         setText(translateIfPossible(err, lang->code()),
596                         Font(inherit_font, lang), false);
597 }
598
599
600 void InsetInfo::setText(docstring const & str, Language const * lang)
601 {
602         setText(str, Font(inherit_font, lang), false);
603 }
604
605
606 bool InsetInfo::forceLTR() const
607 {
608         return force_ltr_;
609 }
610
611
612 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
613         // If the Buffer is a clone, then we neither need nor want to do any
614         // of what follows. We want, rather, just to inherit how things were
615         // in the original Buffer. This is especially important for VCS.
616         // Otherwise, we could in principle have different settings here
617         // than in the Buffer we were exporting.
618         if (buffer().isClone())
619                 return;
620
621         BufferParams const & bp = buffer().params();
622         lang_ = it.paragraph().getFontSettings(bp, it.pos()).language();
623         Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
624         // Some info insets use the language of the GUI (if available)
625         Language const * guilang = tryguilang ? tryguilang : lang_;
626
627         force_ltr_ = !lang_->rightToLeft();
628         // This is just to get the string into the po files
629         docstring gui;
630         switch (type_) {
631         case UNKNOWN_INFO:
632                 gui = _("Unknown Info!");
633                 info(from_ascii("Unknown Info!"), lang_);
634                 initialized_ = false;
635                 break;
636         case SHORTCUT_INFO:
637         case SHORTCUTS_INFO: {
638                 // shortcuts can change, so we need to re-do this each time
639                 FuncRequest const func = lyxaction.lookupFunc(name_);
640                 if (func.action() == LFUN_UNKNOWN_ACTION) {
641                         gui = _("Unknown action %1$s");
642                         error(from_ascii("Unknown action %1$s"), lang_);
643                         break;
644                 }
645                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
646                 if (bindings.empty()) {
647                         gui = _("undefined");
648                         info(from_ascii("undefined"), lang_);
649                         break;
650                 }
651                 if (type_ == SHORTCUT_INFO)
652                         setText(bindings.begin()->print(KeySequence::ForGui), guilang);
653                 else
654                         setText(theTopLevelKeymap().printBindings(func, KeySequence::ForGui), guilang);
655                 force_ltr_ = !guilang->rightToLeft() && !lang_->rightToLeft();
656                 break;
657         }
658         case LYXRC_INFO: {
659                 // this information could change, if the preferences are changed,
660                 // so we will recalculate each time through.
661                 ostringstream oss;
662                 if (name_.empty()) {
663                         gui = _("undefined");
664                         info(from_ascii("undefined"), lang_);
665                         break;
666                 }
667                 // FIXME this uses the serialization mechanism to get the info
668                 // we want, which i guess works but is a bit strange.
669                 lyxrc.write(oss, true, name_);
670                 string result = oss.str();
671                 if (result.size() < 2) {
672                         gui = _("undefined");
673                         info(from_ascii("undefined"), lang_);
674                         break;
675                 }
676                 string::size_type loc = result.rfind("\n", result.size() - 2);
677                 loc = loc == string::npos ? 0 : loc + 1;
678                 if (result.size() < loc + name_.size() + 1
679                           || result.substr(loc + 1, name_.size()) != name_) {
680                         gui = _("undefined");
681                         info(from_ascii("undefined"), lang_);
682                         break;
683                 }
684                 // remove leading comments and \\name and space
685                 result = result.substr(loc + name_.size() + 2);
686
687                 // remove \n and ""
688                 result = rtrim(result, "\n");
689                 result = trim(result, "\"");
690                 setText(from_utf8(result), lang_);
691                 break;
692         }
693         case PACKAGE_INFO:
694                 // only need to do this once.
695                 if (initialized_)
696                         break;
697                 // check in packages.lst
698                 if (LaTeXFeatures::isAvailable(name_)) {
699                         gui = _("yes");
700                         info(from_ascii("yes"), lang_);
701                 } else {
702                         gui = _("no");
703                         info(from_ascii("no"), lang_);
704                 }
705                 initialized_ = true;
706                 break;
707
708         case TEXTCLASS_INFO: {
709                 // the TextClass can change
710                 LayoutFileList const & list = LayoutFileList::get();
711                 bool available = false;
712                 // name_ is the class name
713                 if (list.haveClass(name_))
714                         available = list[name_].isTeXClassAvailable();
715                 if (available) {
716                         gui = _("yes");
717                         info(from_ascii("yes"), lang_);
718                 } else {
719                         gui = _("no");
720                         info(from_ascii("no"), lang_);
721                 }
722                 break;
723         }
724         case MENU_INFO: {
725                 // only need to do this once.
726                 if (initialized_)
727                         break;
728                 // and we will not keep trying if we fail
729                 initialized_ = true;
730                 docstring_list names;
731                 FuncRequest const func = lyxaction.lookupFunc(name_);
732                 if (func.action() == LFUN_UNKNOWN_ACTION) {
733                         gui = _("Unknown action %1$s");
734                         error(from_ascii("Unknown action %1$s"), lang_);
735                         break;
736                 }
737                 // iterate through the menubackend to find it
738                 if (!theApp()) {
739                         gui = _("Can't determine menu entry for action %1$s in batch mode");
740                         error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), lang_);
741                         break;
742                 }
743                 if (!theApp()->searchMenu(func, names)) {
744                         gui = _("No menu entry for action %1$s");
745                         error(from_ascii("No menu entry for action %1$s"), lang_);
746                         break;
747                 }
748                 // if found, return its path.
749                 clear();
750                 Paragraph & par = paragraphs().front();
751                 Font const f(inherit_font, guilang);
752                 force_ltr_ = !guilang->rightToLeft();
753                 //Font fu = f;
754                 //fu.fontInfo().setUnderbar(FONT_ON);
755                 for (docstring const & name : names) {
756                         // do not insert > for the top level menu item
757                         if (&name != &names.front())
758                                 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
759                                                 f, Change(Change::UNCHANGED));
760                         //FIXME: add proper underlines here. This
761                         // involves rewriting searchMenu used above to
762                         // return a vector of menus. If we do not do
763                         // that, we might as well use below
764                         // Paragraph::insert on each string (JMarc)
765                         for (char_type c : name)
766                                 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
767                 }
768                 break;
769         }
770         case ICON_INFO: {
771                 // only need to do this once.
772                 if (initialized_)
773                         break;
774                 // and we will not keep trying if we fail
775                 initialized_ = true;
776                 FuncRequest func = lyxaction.lookupFunc(name_);
777                 docstring icon_name = frontend::Application::iconName(func, true);
778                 // FIXME: We should use the icon directly instead of
779                 // going through FileName. The code below won't work
780                 // if the icon is embedded in the executable through
781                 // the Qt resource system.
782                 // This is only a negligible performance problem:
783                 // If the installed icon differs from the resource icon the
784                 // installed one is preferred anyway, and all icons that are
785                 // embedded in the resources are installed as well.
786                 FileName file(to_utf8(icon_name));
787                 if (file.onlyFileNameWithoutExt() == "unknown") {
788                         string dir = "images";
789                         FileName file2(imageLibFileSearch(dir, name_, "svgz,png"));
790                         if (!file2.empty())
791                                 file = file2;
792                 }
793                 if (!file.exists())
794                         break;
795                 int percent_scale = 100;
796                 if (use_gui) {
797                         // Compute the scale factor for the icon such that its
798                         // width on screen is equal to 1em in pixels.
799                         // The scale factor is rounded to the integer nearest
800                         // to the float value of the ratio 100*iconsize/imgsize.
801                         int imgsize = QImage(toqstr(file.absFileName())).width();
802                         if (imgsize > 0) {
803                                 int iconsize = Length(1, Length::EM).inPixels(1);
804                                 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
805                         }
806                 }
807                 InsetGraphics * inset = new InsetGraphics(buffer_);
808                 InsetGraphicsParams igp;
809                 igp.filename = file;
810                 igp.lyxscale = percent_scale;
811                 igp.scale = string();
812                 igp.width = Length(1, Length::EM);
813                 inset->setParams(igp);
814                 clear();
815                 Font const f(inherit_font, lang_);
816                 paragraphs().front().insertInset(0, inset, f,
817                                                  Change(Change::UNCHANGED));
818                 break;
819         }
820         case BUFFER_INFO: {
821                 // this could all change, so we will recalculate each time
822                 if (name_ == "name")
823                         setText(from_utf8(buffer().fileName().onlyFileName()), lang_);
824                 else if (name_ == "path")
825                         setText(from_utf8(os::latex_path(buffer().filePath())), lang_);
826                 else if (name_ == "class")
827                         setText(from_utf8(bp.documentClass().name()), lang_);
828                 break;
829         }
830         case VCS_INFO: {
831                 // this information could change, in principle, so we will 
832                 // recalculate each time through
833                 if (!buffer().lyxvc().inUse()) {
834                         gui = _("No version control!");
835                         info(from_ascii("No version control!"), lang_);
836                         break;
837                 }
838                 LyXVC::RevisionInfo itype = LyXVC::Unknown;
839                 if (name_ == "revision")
840                         itype = LyXVC::File;
841                 else if (name_ == "tree-revision")
842                         itype = LyXVC::Tree;
843                 else if (name_ == "author")
844                         itype = LyXVC::Author;
845                 else if (name_ == "time")
846                         itype = LyXVC::Time;
847                 else if (name_ == "date")
848                         itype = LyXVC::Date;
849                 string binfo = buffer().lyxvc().revisionInfo(itype);
850                 if (binfo.empty()) {
851                         gui = _("%1$s[[vcs data]] unknown");
852                         error(from_ascii("%1$s[[vcs data]] unknown"), lang_);
853                 } else
854                         setText(from_utf8(binfo), lang_);
855                 break;
856         }
857         case LYX_INFO:
858                 // only need to do this once.
859                 if (initialized_)
860                         break;
861                 if (name_ == "version")
862                         setText(from_ascii(lyx_version), lang_);
863                 initialized_ = true;
864                 break;
865         case DATE_INFO:
866         case MODDATE_INFO:
867         case FIXDATE_INFO: {
868                 string date_format = name_;
869                 string const date_specifier = (type_ == FIXDATE_INFO && contains(name_, '@'))
870                                 ? split(name_, date_format, '@') : string();
871                 QDate date;
872                 if (type_ == MODDATE_INFO)
873                         date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
874                 else if (type_ == FIXDATE_INFO && !date_specifier.empty())
875                         date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
876                 else
877                         date = QDate::currentDate();
878                 setText(getDate(date_format, date), lang_);
879         }
880         }
881         // Just to do something with that string
882         LYXERR(Debug::INFO, "info inset text: " << gui);
883         InsetCollapsible::updateBuffer(it, utype);
884 }
885
886
887 string InsetInfo::contextMenu(BufferView const &, int, int) const
888 {
889         //FIXME: We override the implementation of InsetCollapsible,
890         //because this inset is not a collapsible inset.
891         return contextMenuName();
892 }
893
894
895 string InsetInfo::contextMenuName() const
896 {
897         return "context-info";
898 }
899
900
901 } // namespace lyx