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