xref: /glogg/src/main.cpp (revision 0f9fd9edcfb5309b1c93743600d24be24fc4d684)
1 /*
2  * Copyright (C) 2009, 2010, 2011, 2013 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <QApplication>
21 
22 #include <memory>
23 
24 #include <boost/program_options.hpp>
25 namespace po = boost::program_options;
26 
27 #include <iostream>
28 using namespace std;
29 
30 #include "persistentinfo.h"
31 #include "sessioninfo.h"
32 #include "configuration.h"
33 #include "filterset.h"
34 #include "recentfiles.h"
35 #include "session.h"
36 #include "mainwindow.h"
37 #include "savedsearches.h"
38 #include "log.h"
39 
40 static void print_version();
41 
42 int main(int argc, char *argv[])
43 {
44     QApplication app(argc, argv);
45 
46     string filename = "";
47 
48     TLogLevel logLevel = logWARNING;
49 
50     try {
51         po::options_description desc("Usage: glogg [options] [file]");
52         desc.add_options()
53             ("help,h", "print out program usage (this message)")
54             ("version,v", "print glogg's version information")
55             ("debug,d", "output more debug (include multiple times for more verbosity e.g. -dddd")
56             ;
57         po::options_description desc_hidden("Hidden options");
58         // For -dd, -ddd...
59         for ( string s = "dd"; s.length() <= 10; s.append("d") )
60             desc_hidden.add_options()(s.c_str(), "debug");
61 
62         desc_hidden.add_options()
63             ("input-file", po::value<string>(), "input file")
64             ;
65 
66         po::options_description all_options("all options");
67         all_options.add(desc).add(desc_hidden);
68 
69         po::positional_options_description positional;
70         positional.add("input-file", 1);
71 
72         int command_line_style = (((po::command_line_style::unix_style ^
73                 po::command_line_style::allow_guessing) |
74                 po::command_line_style::allow_long_disguise) ^
75                 po::command_line_style::allow_sticky);
76 
77         po::variables_map vm;
78         po::store(po::command_line_parser(argc, argv).
79                 options(all_options).
80                 positional(positional).
81                 style(command_line_style).run(),
82                 vm);
83         po::notify(vm);
84 
85         if ( vm.count("help") ) {
86             desc.print(cout);
87             return 0;
88         }
89 
90         if ( vm.count("version") ) {
91             print_version();
92             return 0;
93         }
94 
95         if ( vm.count( "debug" ) ) {
96             logLevel = logINFO;
97         }
98 
99         for ( string s = "dd"; s.length() <= 10; s.append("d") )
100             if ( vm.count( s ) )
101                 logLevel = (TLogLevel) (logWARNING + s.length());
102 
103         if ( vm.count("input-file") )
104             filename = vm["input-file"].as<string>();
105     }
106     catch(exception& e) {
107         cerr << "Option processing error: " << e.what() << endl;
108         return 1;
109     }
110     catch(...) {
111         cerr << "Exception of unknown type!\n";
112     }
113 
114 #if 0
115     FILE* file = fopen("glogg.log", "w");
116     Output2FILE::Stream() = file;
117 #endif
118 
119     FILELog::setReportingLevel( logLevel );
120 
121     // Register the configuration items
122     GetPersistentInfo().migrateAndInit();
123     GetPersistentInfo().registerPersistable(
124             new SessionInfo, QString( "session" ) );
125     GetPersistentInfo().registerPersistable(
126             new Configuration, QString( "settings" ) );
127     GetPersistentInfo().registerPersistable(
128             new FilterSet, QString( "filterSet" ) );
129     GetPersistentInfo().registerPersistable(
130             new SavedSearches, QString( "savedSearches" ) );
131     GetPersistentInfo().registerPersistable(
132             new RecentFiles, QString( "recentFiles" ) );
133 
134     // FIXME: should be replaced by a two staged init of MainWindow
135     GetPersistentInfo().retrieve( QString( "settings" ) );
136 
137     std::unique_ptr<Session> session( new Session() );
138     MainWindow mw( std::move( session ) );
139 
140     LOG(logDEBUG) << "MainWindow created.";
141     mw.show();
142     mw.reloadSession();
143     mw.loadInitialFile( QString::fromStdString( filename ) );
144     return app.exec();
145 }
146 
147 static void print_version()
148 {
149     cout << "glogg " GLOGG_VERSION "\n";
150 #ifdef GLOGG_COMMIT
151     cout << "Built " GLOGG_DATE " from " GLOGG_COMMIT "\n";
152 #endif
153     cout << "Copyright (C) 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors\n";
154     cout << "This is free software.  You may redistribute copies of it under the terms of\n";
155     cout << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n";
156     cout << "There is NO WARRANTY, to the extent permitted by law.\n";
157 }
158