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