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