"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2b/settings.cpp" (12 Feb 2021, 27358 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "settings.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #include "settings.h"
    8 
    9 #include "cb2bib_utilities.h"
   10 
   11 #include <QCoreApplication>
   12 #include <QDir>
   13 
   14 
   15 /**
   16     Global settings for cb2Bib
   17 */
   18 settings::settings(const QString& organization, const QString& application, QObject* parento)
   19     : QSettings(organization, application, parento)
   20 {
   21     _init();
   22 }
   23 
   24 settings::settings(const QString& fn, QObject* parento) : QSettings(fn, QSettings::IniFormat, parento)
   25 {
   26     _init();
   27 }
   28 
   29 settings::settings(QObject* parento) : QSettings(C2B_ORGANIZATION, C2B_APPLICATION, parento)
   30 {
   31     _init();
   32     init(false);
   33 }
   34 
   35 settings::~settings()
   36 {
   37     _c2b_global_settings = 0;
   38 }
   39 
   40 
   41 void settings::init(bool command_line)
   42 {
   43     if (command_line)
   44         readCommandLine();
   45     if (cl_user_configured)
   46         _is_usb_configured_mode =
   47             QDir::toNativeSeparators(QDir::cleanPath(QSettings::fileName())) ==
   48             QDir::toNativeSeparators(QDir::cleanPath(_c2bApp->applicationDirPath() + '/' + "cb2bib.conf"));
   49     else
   50         _is_usb_configured_mode = false;
   51     if (_is_usb_configured_mode)
   52     {
   53         _c2bAppDir = QDir(_c2bApp->applicationDirPath());
   54         _tmp_dir_path = _c2bApp->applicationDirPath();
   55     }
   56     else
   57     {
   58         // Use user's home as tmp_dir since tmp files are more accessible if they are needed,
   59         // for instance, for debugging netqinf
   60         // Using cb2bib remotely causes tmp_dir not being writable, fallback to machine's tmp
   61         const QFileInfo hpi(QDir::homePath());
   62         if (hpi.exists() && hpi.isWritable())
   63             _tmp_dir_path = QDir::homePath();
   64         else
   65             _tmp_dir_path = QDir::tempPath();
   66     }
   67     setDefaults();
   68     // Set relative to BibTeX file in USB mode, overriding defaults.
   69     if (_is_usb_configured_mode)
   70         setValue("cb2Bib/RelativePdfDirectory", true);
   71 }
   72 
   73 void settings::_init()
   74 {
   75     _c2bApp = QCoreApplication::instance();
   76     Q_ASSERT_X(_c2bApp, "settings", "application was not instantiated");
   77     Q_ASSERT_X(!_c2b_global_settings, "settings", "settings already instantiated");
   78     settings::_c2b_global_settings = this;
   79     _application_pid = QString::number(_c2bApp->applicationPid());
   80 
   81     _is_loaded = true;
   82     cl_bibedit = false;
   83     cl_cb2bib_import_mode = false;
   84     cl_cb2bib_information = false;
   85     cl_cb2bib_tests = false;
   86     cl_citer = false;
   87     cl_configure = false;
   88     cl_doc2bib = false;
   89     cl_html_annote = false;
   90     cl_import = false;
   91     cl_index = false;
   92     cl_sloppy = false;
   93     cl_txt2bib = false;
   94     cl_user_configured = false;
   95     cl_view_annote = false;
   96 }
   97 
   98 void settings::setDefaultValue(const QString& key, const QVariant& defaulValue)
   99 {
  100     // Makes sure a key is initialized
  101     setValue(key, value(key, defaulValue));
  102 }
  103 
  104 void settings::setDefaults()
  105 {
  106     QHash<QString, QVariant> defaults;
  107     const QString dsep(QDir::separator());
  108 
  109     // Files
  110     defaults["cb2Bib/BibTeXFile"] = QDir::cleanPath(QDir::homePath() + dsep + "references.bib");
  111     defaults["cb2Bib/CacheDirectory"] = QDir::cleanPath(QDir::homePath() + dsep + "PdfDocuments" + dsep + "c2b.tmp");
  112     defaults["cb2Bib/PdfDirectory"] = QDir::cleanPath(QDir::homePath() + dsep + "PdfDocuments");
  113 
  114 #if defined(Q_OS_WIN)
  115     const QString C2B_APP_DIR(_c2bApp->applicationDirPath() + dsep);
  116     defaults["c2bAnnote/AnnoteCssFile"] = C2B_APP_DIR + "data\\tex2html.css";
  117     defaults["c2bAnnote/MathJaxHeaderFile"] = C2B_APP_DIR + "data\\tex2html_mathjax_header.html";
  118     defaults["c2bBibSearcher/Pdf2TextBin"] = C2B_APP_DIR + "externtools\\xpdf\\pdf2cb.exe";
  119     defaults["c2bPdfImport/LastFile"] = C2B_APP_DIR + "testPDFImport\\nomeaning1.pdf";
  120     defaults["c2bPdfImport/Pdf2TextBin"] = C2B_APP_DIR + "externtools\\xpdf\\pdf2cb.exe";
  121     defaults["cb2Bib/BrowserCssFile"] = C2B_APP_DIR + "data\\references.css";
  122     defaults["cb2Bib/JournalFile"] = C2B_APP_DIR + "data\\abbreviations.txt";
  123     defaults["cb2Bib/NetworkFile"] = C2B_APP_DIR + "data\\netqinf.txt";
  124     defaults["cb2Bib/PosLexiconFile"] = C2B_APP_DIR + "data\\lexicon.pos";
  125     defaults["cb2Bib/PostprocessBibTeXBin"] = C2B_APP_DIR + C2B_POSTPROCESS_BIBTEX_BIN;
  126     defaults["cb2Bib/PreparserBin"] = C2B_APP_DIR + "c2btools\\ris2bib.bat";
  127     defaults["cb2Bib/RegularExpressionFile"] = C2B_APP_DIR + "data\\regexps.txt";
  128 #elif defined(Q_OS_MACOS)
  129     QString C2B_DATA_DIR_MAC(C2B_DATA_DIR);
  130     if (QDir(C2B_DATA_DIR + "/data/").exists())
  131         C2B_DATA_DIR_MAC = QDir::cleanPath(C2B_DATA_DIR + '/');
  132     else if (QDir(_c2bApp->applicationDirPath() + "/../Resources/data/").exists())
  133         C2B_DATA_DIR_MAC = QDir::cleanPath(_c2bApp->applicationDirPath() + "/../Resources/");
  134     defaults["c2bAnnote/AnnoteCssFile"] = QDir::cleanPath(C2B_DATA_DIR_MAC + "/data/tex2html.css");
  135     defaults["c2bAnnote/MathJaxHeaderFile"] =
  136         QDir::cleanPath(C2B_DATA_DIR_MAC + "/data/tex2html_local_mathjax_header.html");
  137     defaults["c2bBibSearcher/Pdf2TextBin"] = QDir::cleanPath(C2B_BIBSEARCHER_PDF2TXT_BIN);
  138     defaults["c2bPdfImport/LastFile"] = QDir::cleanPath(C2B_DATA_DIR_MAC + "/testPDFImport/nomeaning1.pdf");
  139     defaults["c2bPdfImport/Pdf2TextBin"] = QDir::cleanPath(C2B_PDFIMPORT_PDF2TXT_BIN);
  140     defaults["cb2Bib/BrowserCssFile"] = QDir::cleanPath(C2B_DATA_DIR_MAC + "/data/references.css");
  141     defaults["cb2Bib/ExifToolBin"] = QDir::cleanPath(C2B_METADATAPARSER_EXIFTOOL_BIN);
  142     defaults["cb2Bib/JournalFile"] = QDir::cleanPath(C2B_DATA_DIR_MAC + "/data/abbreviations.txt");
  143     defaults["cb2Bib/NetworkFile"] = QDir::cleanPath(C2B_DATA_DIR_MAC + "/data/netqinf.txt");
  144     defaults["cb2Bib/PosLexiconFile"] = QDir::cleanPath(C2B_DATA_DIR_MAC + dsep + "data/lexicon.pos");
  145     defaults["cb2Bib/PostprocessBibTeXBin"] = QDir::cleanPath(C2B_DATA_DIR_MAC + C2B_POSTPROCESS_BIBTEX_BIN);
  146     defaults["cb2Bib/PreparserBin"] = QDir::cleanPath(C2B_DATA_DIR_MAC + "/c2btools/ris2bib");
  147     defaults["cb2Bib/RegularExpressionFile"] = QDir::cleanPath(C2B_DATA_DIR_MAC + "/data/regexps.txt");
  148 #else // Q_OS_UNIX
  149     defaults["c2bAnnote/AnnoteCssFile"] = QDir::cleanPath(C2B_DATA_DIR + dsep + "data/tex2html.css");
  150     defaults["c2bAnnote/MathJaxHeaderFile"] =
  151         QDir::cleanPath(C2B_DATA_DIR + dsep + "data/tex2html_local_mathjax_header.html");
  152     defaults["c2bBibSearcher/Pdf2TextBin"] = QDir::cleanPath(C2B_BIBSEARCHER_PDF2TXT_BIN);
  153     defaults["c2bPdfImport/LastFile"] = QDir::cleanPath(C2B_DATA_DIR + dsep + "testPDFImport/nomeaning1.pdf");
  154     defaults["c2bPdfImport/Pdf2TextBin"] = QDir::cleanPath(C2B_PDFIMPORT_PDF2TXT_BIN);
  155     defaults["cb2Bib/BrowserCssFile"] = QDir::cleanPath(C2B_DATA_DIR + dsep + "data/references.css");
  156     defaults["cb2Bib/ExifToolBin"] = QDir::cleanPath(C2B_METADATAPARSER_EXIFTOOL_BIN);
  157     defaults["cb2Bib/JournalFile"] = QDir::cleanPath(C2B_DATA_DIR + dsep + "data/abbreviations.txt");
  158     defaults["cb2Bib/NetworkFile"] = QDir::cleanPath(C2B_DATA_DIR + dsep + "data/netqinf.txt");
  159     defaults["cb2Bib/PosLexiconFile"] = QDir::cleanPath(C2B_DATA_DIR + dsep + "data/lexicon.pos");
  160     defaults["cb2Bib/PostprocessBibTeXBin"] = QDir::cleanPath(C2B_DATA_DIR + dsep + C2B_POSTPROCESS_BIBTEX_BIN);
  161     defaults["cb2Bib/PreparserBin"] = QDir::cleanPath(C2B_DATA_DIR + dsep + "c2btools/ris2bib");
  162     defaults["cb2Bib/RegularExpressionFile"] = QDir::cleanPath(C2B_DATA_DIR + dsep + "data/regexps.txt");
  163 #endif
  164 
  165     // External file manager
  166     defaults["cb2Bib/FmClient"] = false;
  167     defaults["cb2Bib/FmClientCopyArg"] = C2B_FM_CLIENT_COPY_ARG;
  168     defaults["cb2Bib/FmClientCopyBin"] = QDir::cleanPath(C2B_FM_CLIENT_COPY_BIN);
  169     defaults["cb2Bib/FmClientMoveArg"] = C2B_FM_CLIENT_MOVE_ARG;
  170     defaults["cb2Bib/FmClientMoveBin"] = QDir::cleanPath(C2B_FM_CLIENT_MOVE_BIN);
  171 
  172     // Proxy
  173     defaults["cb2Bib/UseProxy"] = false;
  174     defaults["cb2Bib/ProxyPort"] = 8080;
  175     defaults["cb2Bib/ProxyType"] = 0;
  176 
  177     // Postprocess BibTeX
  178     defaults["cb2Bib/PostprocessBibTeXArg"] = C2B_POSTPROCESS_BIBTEX_ARG;
  179     defaults["cb2Bib/PostprocessBibTeXExt"] = C2B_POSTPROCESS_BIBTEX_EXT;
  180 
  181     // Misc
  182     defaults["c2bAnnote/IncludeCSS"] = true;
  183     defaults["c2bAnnote/UseRelativeLinks"] = false;
  184     defaults["cb2Bib/AddMetadata"] = true;
  185     defaults["cb2Bib/AutomaticPdfDownload"] = false;
  186     defaults["cb2Bib/AutomaticQuery"] = false;
  187     defaults["cb2Bib/CheckRepeatedOnSave"] = false;
  188     defaults["cb2Bib/CiteCommandPattern"] = C2B_CITE_COMMAND_PATTERN;
  189     defaults["cb2Bib/CiteIdPattern"] = C2B_CITE_ID_PATTERN;
  190     defaults["cb2Bib/ConvertReferenceToLaTeX"] = true;
  191     defaults["cb2Bib/CopyMovePdf"] = "copy";
  192     defaults["cb2Bib/DoHeuristicGuess"] = true;
  193     defaults["cb2Bib/DocumentIdPattern"] = C2B_DOCUMENT_ID_PATTERN;
  194     defaults["cb2Bib/FindReplaceInput"] = true;
  195     defaults["cb2Bib/FindReplaceInputList"] = QString(C2B_FIND_REPLACE_LIST).split("^e");
  196     defaults["cb2Bib/InsertMetadata"] = false;
  197     defaults["cb2Bib/KeepTmpNQFiles"] = false;
  198     defaults["cb2Bib/MovePdf"] = false;
  199     defaults["cb2Bib/PageNumberSeparator"] = " - ";
  200     defaults["cb2Bib/PostprocessMonth"] = false;
  201     defaults["cb2Bib/PreAppendMetadata"] = "prepend";
  202     defaults["cb2Bib/PreparserAutomatic"] = false;
  203     defaults["cb2Bib/PreparserShowLog"] = true;
  204     defaults["cb2Bib/RelativePdfDirectory"] = false;
  205     defaults["cb2Bib/SetJournalsToFullname"] = false;
  206     defaults["cb2Bib/UseDoubleBraces"] = true;
  207     defaults["cb2Bib/UseFullNames"] = false;
  208 
  209     // Pdf2Text arguments
  210     defaults["c2bBibSearcher/Pdf2TextArg"] = "-q";
  211     defaults["c2bPdfImport/Pdf2TextArg"] = "-q -f 1 -l 1";
  212 
  213     // Construct current settings
  214     const QStringList keys(defaults.keys());
  215     for (int i = 0; i < keys.count(); ++i)
  216         setValue(keys.at(i), value(keys.at(i), defaults.value(keys.at(i))));
  217 }
  218 
  219 QString settings::applicationDataDir() const
  220 {
  221 // Base application data dir containing ./data/ ./doc/ ./c2btools/
  222 #if defined(Q_OS_WIN)
  223     const QString app_data_dir(_c2bApp->applicationDirPath());
  224 #elif defined(Q_OS_MACOS)
  225     QString app_data_dir(C2B_DATA_DIR);
  226     if (!QDir(app_data_dir).exists())
  227         app_data_dir = _c2bApp->applicationDirPath() + "/../Resources/";
  228 #else // Q_OS_UNIX
  229     const QString app_data_dir(C2B_DATA_DIR);
  230 #endif
  231     if (QDir(app_data_dir + "/data/").exists())
  232         return QDir::cleanPath(app_data_dir);
  233     else
  234         return QString();
  235 }
  236 
  237 settings* settings::_c2b_global_settings = 0;
  238 
  239 
  240 /***************************************************************************
  241  *  Command line processing
  242  ***************************************************************************/
  243 
  244 /** \page commandline cb2Bib Command Line
  245 
  246 
  247   The complete listing of command line uses follows.
  248 
  249 
  250 \htmlonly
  251 <pre style="font-size: xx-small;">
  252  Usage: cb2bib
  253         cb2bib [action] [filename1 [filename2 ... ]] [--conf [filename.conf]]
  254 
  255  Actions:
  256         --configure [filename.conf]                        Edit configuration
  257 
  258         --bibedit [filename1.bib [filename2.bib ... ]]     Edit/browse BibTeX files
  259         --citer [filename1.bib   [filename2.bib ... ]]     Start cb2Bib citer
  260         --import tmp_reference_filename                    Import reference, usually from ad hoc websites
  261 
  262         --doc2bib fn1.doc [fn2.doc ... ] reference.bib     Extract reference from document file
  263         --txt2bib fn1.txt [fn2.txt ... ] reference.bib     Extract reference from text file
  264 
  265         --index [bibdirname]                               Extract keywords from document files
  266 
  267         --html-annote filename.tex                         Convert annote file to HTML
  268         --view-annote filename.tex                         Convert and visualize annote file
  269         --view-annote filename.tex.html                    Visualize annote file
  270 
  271  Switches:
  272         --conf [filename.conf]                             Use configuration file
  273         --sloppy                                           Accept guesses in automatic reference extraction
  274 
  275  Examples:
  276         cb2bib                                             Start cb2Bib extraction panel
  277         cb2bib --import tmp_reference_filename             Import reference
  278         cb2bib --bibedit filename.bib                      Edit BibTeX filename.bib
  279         cb2bib --conf                                      Start cb2Bib in USB mode
  280         cb2bib --doc2bib *.pdf references.bib              Extract references from PDF title pages
  281 
  282  Notes:
  283   -Use switch --conf to particularize specific settings for specific actions.
  284   -The file cb2bib.conf must be readable and writable. If it does not exist, cb2Bib will create one
  285    based on predefined defaults.
  286   -If starting cb2Bib from a removable media, use the command 'cb2bib --conf' without configuration
  287    filename. Settings will be read from and written to /cb2bib/full/path/cb2bib.conf, being therefore
  288    independent of the mounting address that the host computer will provide.
  289   -To import references from a browser select when asked c2bimport, which expands to 'cb2bib --import %f'.
  290    The browser will provide the temporary reference filename.
  291   -A number of factors influence the reliability of automatic extractions. Consider writing customized
  292    regular expressions and network queries, and use metadata when available.
  293 
  294  Important:
  295   -The commands --doc2bib and --txt2bib do not append the references to the references.bib. They create
  296    a new file, or silently overwrite it if already exists.
  297 
  298 </pre>
  299 \endhtmlonly
  300 
  301 
  302  - <b>Note:</b> On Windows use <tt>c2bconsole</tt> instead of <tt>cb2bib</tt>.
  303  See \ref relnotes130.
  304 
  305  - <b>Note:</b> If using reference extraction command, see \ref
  306  faq_automatic_extraction.
  307 
  308 */
  309 
  310 bool settings::isConsoleMode(const int argc, char** argv)
  311 {
  312     for (int i = 0; i < argc; ++i)
  313         if (strcmp(argv[i], "--html-annote") == 0 || strcmp(argv[i], "--doc2bib") == 0 ||
  314             strcmp(argv[i], "--txt2bib") == 0 || strcmp(argv[i], "--index") == 0 || strcmp(argv[i], "--c2binfo") == 0 ||
  315             strcmp(argv[i], "--test") == 0)
  316             return true;
  317     return false;
  318 }
  319 
  320 bool settings::isSingleApplicationMode(const int argc, char** argv)
  321 {
  322     for (int i = 0; i < argc; ++i)
  323         if (strcmp(argv[i], "--citer") == 0)
  324             return true;
  325     return false;
  326 }
  327 
  328 bool settings::isConfiguredMode()
  329 {
  330     const QCoreApplication* capp = QCoreApplication::instance();
  331     const QStringList argv(capp->arguments());
  332     const int argc(argv.count());
  333     if (argc > 1)
  334     {
  335         for (int i = 1; i < argc; ++i)
  336             if (argv.at(i) == "--conf")
  337                 return true;
  338         for (int i = 1; i < argc; ++i)
  339             if (argv.at(i) == "--configure")
  340             {
  341                 const int j(i + 1);
  342                 if (j < argc)
  343                     if (!argv.at(j).startsWith('-'))
  344                         return true;
  345                 return false;
  346             }
  347     }
  348     return false;
  349 }
  350 
  351 const QString settings::configuredFilename()
  352 {
  353     const QCoreApplication* capp = QCoreApplication::instance();
  354     QStringList arguments(capp->arguments());
  355     arguments.removeAll(QString());
  356     const int argc(arguments.count());
  357     QString filename;
  358     for (int i = 1; i < argc - 1; ++i)
  359         if (arguments.at(i) == "--conf" || arguments.at(i) == "--configure")
  360         {
  361             const int j(i + 1);
  362             if (!arguments.at(j).startsWith('-'))
  363                 filename = QFile::decodeName(arguments.at(j).toLocal8Bit());
  364             break;
  365         }
  366     if (filename.isEmpty())
  367         filename = capp->applicationDirPath() + QDir::separator() + "cb2bib.conf";
  368     QFileInfo fi(filename);
  369     if (fi.isDir())
  370     {
  371         c2bUtils::warn(QObject::tr("Error: Could not open %1 configuration file for reading/writing").arg(filename));
  372         c2bUtils::warn(QObject::tr("Error: Filename is a directory"));
  373         exit(1);
  374     }
  375     QFile file(filename);
  376     if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
  377     {
  378         c2bUtils::warn(QObject::tr("Error: Could not open %1 configuration file for reading/writing").arg(filename));
  379         c2bUtils::warn(QObject::tr("Error: %1.").arg(file.errorString()));
  380         exit(1);
  381     }
  382     return filename;
  383 }
  384 
  385 void settings::readCommandLine()
  386 {
  387     QStringList arguments(_c2bApp->arguments());
  388     arguments.removeAll(QString());
  389     int actions(0);
  390     for (int i = 1; i < arguments.count(); ++i)
  391         if (arguments.at(i).startsWith('-'))
  392         {
  393             // switches
  394             if (arguments.at(i) == "--conf")
  395             {
  396                 checkCommand(&cl_user_configured, &actions);
  397                 _conf_command_flags.append("--conf");
  398                 const int j(i + 1);
  399                 if (j < arguments.count() && !arguments.at(j).startsWith('-'))
  400                     _conf_command_flags.append(arguments.at(j));
  401                 --actions;
  402             }
  403             else if (arguments.at(i) == "--sloppy")
  404             {
  405                 checkCommand(&cl_sloppy, &actions);
  406                 --actions;
  407             }
  408             // actions
  409             else if (arguments.at(i) == "--configure")
  410                 checkCommand(&cl_configure, &actions);
  411             else if (arguments.at(i) == "--bibedit")
  412                 checkCommand(&cl_bibedit, &actions);
  413             else if (arguments.at(i) == "--citer")
  414                 checkCommand(&cl_citer, &actions);
  415             else if (arguments.at(i) == "--import")
  416                 checkCommand(&cl_import, &actions);
  417             else if (arguments.at(i) == "--html-annote")
  418                 checkCommand(&cl_html_annote, &actions);
  419             else if (arguments.at(i) == "--view-annote")
  420                 checkCommand(&cl_view_annote, &actions);
  421             else if (arguments.at(i) == "--doc2bib")
  422                 checkCommand(&cl_doc2bib, &actions);
  423             else if (arguments.at(i) == "--txt2bib")
  424                 checkCommand(&cl_txt2bib, &actions);
  425             else if (arguments.at(i) == "--index")
  426                 checkCommand(&cl_index, &actions);
  427             else if (arguments.at(i) == "--c2binfo")
  428                 cl_cb2bib_information = true;
  429             else if (arguments.at(i).startsWith("--test"))
  430                 cl_cb2bib_tests = true;
  431             else
  432                 _is_loaded = false;
  433         }
  434     if (actions > 1)
  435         _is_loaded = false;
  436     if (cl_user_configured && cl_configure)
  437         _is_loaded = false;
  438     if (cl_sloppy && !(cl_doc2bib || cl_txt2bib))
  439         _is_loaded = false;
  440     if (cl_index &&
  441         (cl_bibedit || cl_citer || cl_import || cl_html_annote || cl_view_annote || cl_doc2bib || cl_txt2bib))
  442         _is_loaded = false;
  443     if (!_is_loaded)
  444     {
  445         printUsage();
  446         return;
  447     }
  448     cl_console_mode =
  449         cl_html_annote || cl_cb2bib_information || cl_cb2bib_tests || cl_doc2bib || cl_txt2bib || cl_index;
  450     cl_cb2bib_mode = !(cl_console_mode || cl_configure || cl_view_annote);
  451 
  452     // Check for cl_user_configured
  453     if (cl_configure)
  454         for (int i = 1; i < arguments.count() - 1; ++i)
  455             if (arguments.at(i) == "--configure")
  456             {
  457                 if (!arguments.at(i + 1).startsWith('-'))
  458                     cl_user_configured = true;
  459                 break;
  460             }
  461     // Check for --bibedit files
  462     if (cl_bibedit)
  463     {
  464         for (int i = 1; i < arguments.count(); ++i)
  465             if (arguments.at(i) == "--bibedit")
  466             {
  467                 for (int j = i + 1; j < arguments.count(); ++j)
  468                     if (arguments.at(j).startsWith('-'))
  469                         break;
  470                     else
  471                         cl_bibedit_filenames.append(decodeFilename(arguments.at(j)));
  472                 break;
  473             }
  474         if (cl_bibedit_filenames.count() == 0)
  475             cl_bibedit = false;
  476         else
  477             setValue("cb2Bib/BibTeXFile", cl_bibedit_filenames.first());
  478     }
  479     // Check for --citer files
  480     else if (cl_citer)
  481     {
  482         for (int i = 1; i < arguments.count(); ++i)
  483             if (arguments.at(i) == "--citer")
  484             {
  485                 for (int j = i + 1; j < arguments.count(); ++j)
  486                     if (arguments.at(j).startsWith('-'))
  487                         break;
  488                     else
  489                         cl_citer_filenames.append(decodeFilename(arguments.at(j)));
  490                 break;
  491             }
  492     }
  493     // Check for --index BibTeX dirname
  494     else if (cl_index)
  495     {
  496         for (int i = 1; i < arguments.count(); ++i)
  497             if (arguments.at(i) == "--index")
  498             {
  499                 const int j(i + 1);
  500                 if (j < arguments.count() && !arguments.at(j).startsWith('-'))
  501                     cl_index_dirname.append(decodeFilename(arguments.at(j)));
  502                 break;
  503             }
  504     }
  505     // Check for annote file
  506     else if (cl_html_annote || cl_view_annote)
  507     {
  508         for (int i = 1; i < arguments.count(); ++i)
  509             if (arguments.at(i) == "--html-annote" || arguments.at(i) == "--view-annote")
  510             {
  511                 const int j(i + 1);
  512                 if (j < arguments.count())
  513                     if (!arguments.at(j).startsWith('-'))
  514                         cl_annote_filename = decodeFilename(arguments.at(j));
  515                 if (cl_annote_filename.isEmpty())
  516                     _is_loaded = false;
  517             }
  518     }
  519     // Check for extraction files
  520     else if (cl_doc2bib || cl_txt2bib)
  521     {
  522         for (int i = 1; i < arguments.count(); ++i)
  523             if (arguments.at(i) == "--doc2bib" || arguments.at(i) == "--txt2bib")
  524             {
  525                 for (int j = i + 1; j < arguments.count(); ++j)
  526                     if (arguments.at(j).startsWith('-'))
  527                         break;
  528                     else
  529                         cl_extract_input_filenames.append(decodeFilename(arguments.at(j)));
  530                 break;
  531             }
  532         if (cl_extract_input_filenames.count() < 2)
  533             _is_loaded = false;
  534         else
  535             cl_extract_output_filename = cl_extract_input_filenames.takeLast();
  536     }
  537     // Check for import reference file
  538     else if (cl_import)
  539     {
  540         if (arguments.count() > 2)
  541             if (!arguments.at(2).startsWith('-'))
  542             {
  543                 cl_import_filename = QFileInfo(QFile::decodeName(arguments.at(2).toLocal8Bit())).absoluteFilePath();
  544                 return;
  545             }
  546         cl_import = false;
  547         _is_loaded = false;
  548     }
  549     // Accept also 'cb2bib filename' as '--import' to ease import on Windows
  550     else if (arguments.count() > 1)
  551         if (!arguments.at(1).startsWith('-'))
  552         {
  553             cl_import_filename = QFileInfo(QFile::decodeName(arguments.at(1).toLocal8Bit())).absoluteFilePath();
  554             cl_import = true;
  555             return;
  556         }
  557     if (!_is_loaded)
  558         printUsage();
  559 }
  560 
  561 void settings::checkCommand(bool* action, int* actions)
  562 {
  563     if (*action)
  564         _is_loaded = false;
  565     *action = true;
  566     *actions += 1;
  567 }
  568 
  569 QString settings::decodeFilename(const QString& fn) const
  570 {
  571     // This function is meant for fn given by the command shell and that will end up being visible
  572     return QDir::toNativeSeparators(QFileInfo(QFile::decodeName(fn.toLocal8Bit())).absoluteFilePath());
  573 }
  574 
  575 void settings::printUsage()
  576 {
  577     fprintf(stdout, "Usage: cb2bib\n");
  578     fprintf(stdout, "       cb2bib [action] [filename1 [filename2 ... ]] [--conf [filename.conf]]\n");
  579     fprintf(stdout, "\n");
  580     fprintf(stdout, "Actions:\n");
  581     fprintf(stdout, "       --configure [filename.conf]                        Edit configuration\n");
  582     fprintf(stdout, "\n");
  583     fprintf(stdout, "       --bibedit [filename1.bib [filename2.bib ... ]]     Edit/browse BibTeX files\n");
  584     fprintf(stdout, "       --citer [filename1.bib [filename2.bib ... ]]       Start cb2Bib citer\n");
  585     fprintf(
  586         stdout,
  587         "       --import tmp_reference_filename                    Import reference, usually from ad hoc websites\n");
  588     fprintf(stdout, "\n");
  589     fprintf(stdout, "       --doc2bib fn1.doc [fn2.doc ... ] reference.bib     Extract reference from document file\n");
  590     fprintf(stdout, "       --txt2bib fn1.txt [fn2.txt ... ] reference.bib     Extract reference from text file\n");
  591     fprintf(stdout, "\n");
  592     fprintf(stdout, "       --index [bibdirname]                               Extract keywords from document files\n");
  593     fprintf(stdout, "\n");
  594     fprintf(stdout, "       --html-annote filename.tex                         Convert annote file to HTML\n");
  595     fprintf(stdout, "       --view-annote filename.tex                         Convert and visualize annote file\n");
  596     fprintf(stdout, "       --view-annote filename.tex.html                    Visualize annote file\n");
  597     fprintf(stdout, "\n");
  598     fprintf(stdout, "Switches:\n");
  599     fprintf(stdout, "       --conf [filename.conf]                             Use configuration file\n");
  600     fprintf(
  601         stdout,
  602         "       --sloppy                                           Accept guesses in automatic reference extraction\n");
  603     fprintf(stdout, "\n");
  604     fprintf(stdout, "Examples:\n");
  605     fprintf(stdout, "       cb2bib                                             Start cb2Bib extraction panel\n");
  606     fprintf(stdout, "       cb2bib --import tmp_reference_filename             Import reference\n");
  607     fprintf(stdout, "       cb2bib --bibedit filename.bib                      Edit BibTeX filename.bib\n");
  608     fprintf(stdout, "       cb2bib --conf                                      Start cb2Bib in USB mode\n");
  609     fprintf(stdout,
  610             "       cb2bib --doc2bib *.pdf references.bib              Extract references from PDF title pages\n");
  611     fprintf(stdout, "\n");
  612     fprintf(stdout, "Notes:\n");
  613     fprintf(stdout, " -Use switch --conf to particularize specific settings for specific actions.\n");
  614     fprintf(stdout,
  615             " -The file cb2bib.conf must be readable and writable. If it does not exist, cb2Bib will create one\n");
  616     fprintf(stdout, "  based on predefined defaults.\n");
  617     fprintf(stdout,
  618             " -If starting cb2Bib from a removable media use, the command 'cb2bib --conf' without configuration\n");
  619     fprintf(stdout,
  620             "  filename. Settings will be read from and written to /cb2bib/full/path/cb2bib.conf, being therefore\n");
  621     fprintf(stdout, "  independent of the mounting address that the host computer will provide.\n");
  622     fprintf(
  623         stdout,
  624         " -To import references from a browser select when asked c2bimport, which expands to 'cb2bib --import %%f'.\n");
  625     fprintf(stdout, "  The browser will provide the temporary reference filename.\n");
  626     fprintf(stdout,
  627             " -A number of factors influence the reliability of automatic extractions. Consider writing customized\n");
  628     fprintf(stdout, "  regular expressions and network queries, and use metadata when available.\n");
  629     fprintf(stdout, "\n");
  630     fprintf(stdout, "Important:\n");
  631     fprintf(stdout,
  632             " -The commands --doc2bib and --txt2bib do not append the references to the references.bib. They create\n");
  633     fprintf(stdout, "  a new file, or silently overwrite it if already exists.\n");
  634     fprintf(stdout, "\n");
  635 }