xref: /glogg/src/main.cpp (revision 557fb9d8925f3772c017d921d356ab742ec51021)
1 /*
2  * Copyright (C) 2009, 2010, 2011, 2013, 2014 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 "loadingstatus.h"
39 #include "dbusexternalcom.h"
40 #include "log.h"
41 
42 static void print_version();
43 
44 int main(int argc, char *argv[])
45 {
46     QApplication app(argc, argv);
47 
48     string filename = "";
49 
50     TLogLevel logLevel = logWARNING;
51 
52     try {
53         po::options_description desc("Usage: glogg [options] [file]");
54         desc.add_options()
55             ("help,h", "print out program usage (this message)")
56             ("version,v", "print glogg's version information")
57             ("debug,d", "output more debug (include multiple times for more verbosity e.g. -dddd")
58             ;
59         po::options_description desc_hidden("Hidden options");
60         // For -dd, -ddd...
61         for ( string s = "dd"; s.length() <= 10; s.append("d") )
62             desc_hidden.add_options()(s.c_str(), "debug");
63 
64         desc_hidden.add_options()
65             ("input-file", po::value<string>(), "input file")
66             ;
67 
68         po::options_description all_options("all options");
69         all_options.add(desc).add(desc_hidden);
70 
71         po::positional_options_description positional;
72         positional.add("input-file", 1);
73 
74         int command_line_style = (((po::command_line_style::unix_style ^
75                 po::command_line_style::allow_guessing) |
76                 po::command_line_style::allow_long_disguise) ^
77                 po::command_line_style::allow_sticky);
78 
79         po::variables_map vm;
80         po::store(po::command_line_parser(argc, argv).
81                 options(all_options).
82                 positional(positional).
83                 style(command_line_style).run(),
84                 vm);
85         po::notify(vm);
86 
87         if ( vm.count("help") ) {
88             desc.print(cout);
89             return 0;
90         }
91 
92         if ( vm.count("version") ) {
93             print_version();
94             return 0;
95         }
96 
97         if ( vm.count( "debug" ) ) {
98             logLevel = logINFO;
99         }
100 
101         for ( string s = "dd"; s.length() <= 10; s.append("d") )
102             if ( vm.count( s ) )
103                 logLevel = (TLogLevel) (logWARNING + s.length());
104 
105         if ( vm.count("input-file") )
106             filename = vm["input-file"].as<string>();
107     }
108     catch(exception& e) {
109         cerr << "Option processing error: " << e.what() << endl;
110         return 1;
111     }
112     catch(...) {
113         cerr << "Exception of unknown type!\n";
114     }
115 
116 #if 0
117     FILE* file = fopen("glogg.log", "w");
118     Output2FILE::Stream() = file;
119 #endif
120 
121     FILELog::setReportingLevel( logLevel );
122 
123     ExternalCommunicator* externalCommunicator = new DBusExternalCommunicator();
124     ExternalInstance* externalInstance = externalCommunicator->otherInstance();
125 
126     // Register types for Qt
127     qRegisterMetaType<LoadingStatus>("LoadingStatus");
128 
129     // Register the configuration items
130     GetPersistentInfo().migrateAndInit();
131     GetPersistentInfo().registerPersistable(
132             std::make_shared<SessionInfo>(), QString( "session" ) );
133     GetPersistentInfo().registerPersistable(
134             std::make_shared<Configuration>(), QString( "settings" ) );
135     GetPersistentInfo().registerPersistable(
136             std::make_shared<FilterSet>(), QString( "filterSet" ) );
137     GetPersistentInfo().registerPersistable(
138             std::make_shared<SavedSearches>(), QString( "savedSearches" ) );
139     GetPersistentInfo().registerPersistable(
140             std::make_shared<RecentFiles>(), QString( "recentFiles" ) );
141 
142     // FIXME: should be replaced by a two staged init of MainWindow
143     GetPersistentInfo().retrieve( QString( "settings" ) );
144 
145     std::unique_ptr<Session> session( new Session() );
146     MainWindow mw( std::move( session ) );
147 
148     LOG(logDEBUG) << "MainWindow created.";
149     mw.show();
150     mw.reloadSession();
151     mw.loadInitialFile( QString::fromStdString( filename ) );
152     return app.exec();
153 }
154 
155 static void print_version()
156 {
157     cout << "glogg " GLOGG_VERSION "\n";
158 #ifdef GLOGG_COMMIT
159     cout << "Built " GLOGG_DATE " from " GLOGG_COMMIT "\n";
160 #endif
161     cout << "Copyright (C) 2009, 2010, 2011, 2012, 2013 Nicolas Bonnefon and other contributors\n";
162     cout << "This is free software.  You may redistribute copies of it under the terms of\n";
163     cout << "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n";
164     cout << "There is NO WARRANTY, to the extent permitted by law.\n";
165 }
166