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