| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2012 Ricardo Villalba <rvm@users.sourceforge.net>
|
|---|
| 3 |
|
|---|
| 4 | This program is free software; you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program; if not, write to the Free Software
|
|---|
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | #include <QApplication>
|
|---|
| 20 | #include <QFile>
|
|---|
| 21 | #include <QTime>
|
|---|
| 22 | #include <QDir>
|
|---|
| 23 |
|
|---|
| 24 | #include "smplayer.h"
|
|---|
| 25 | #include "global.h"
|
|---|
| 26 | #include "helper.h"
|
|---|
| 27 | #include "paths.h"
|
|---|
| 28 |
|
|---|
| 29 | #include <stdio.h>
|
|---|
| 30 |
|
|---|
| 31 | #if USE_QTLOCKEDFILE
|
|---|
| 32 | #define USE_LOCKS 1
|
|---|
| 33 | #if USE_LOCKS
|
|---|
| 34 | #include "qtlockedfile/qtlockedfile.h"
|
|---|
| 35 | #endif
|
|---|
| 36 | #endif
|
|---|
| 37 |
|
|---|
| 38 | using namespace Global;
|
|---|
| 39 |
|
|---|
| 40 | BaseGui * basegui_instance = 0;
|
|---|
| 41 |
|
|---|
| 42 | QFile output_log;
|
|---|
| 43 |
|
|---|
| 44 | void myMessageOutput( QtMsgType type, const char *msg ) {
|
|---|
| 45 | static QStringList saved_lines;
|
|---|
| 46 | static QString orig_line;
|
|---|
| 47 | static QString line2;
|
|---|
| 48 | static QRegExp rx_log;
|
|---|
| 49 |
|
|---|
| 50 | if (pref) {
|
|---|
| 51 | if (!pref->log_smplayer) return;
|
|---|
| 52 | rx_log.setPattern(pref->log_filter);
|
|---|
| 53 | } else {
|
|---|
| 54 | rx_log.setPattern(".*");
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | line2.clear();
|
|---|
| 58 |
|
|---|
| 59 | orig_line = QString::fromUtf8(msg);
|
|---|
| 60 |
|
|---|
| 61 | switch ( type ) {
|
|---|
| 62 | case QtDebugMsg:
|
|---|
| 63 | if (rx_log.indexIn(orig_line) > -1) {
|
|---|
| 64 | #ifndef NO_DEBUG_ON_CONSOLE
|
|---|
| 65 | fprintf( stderr, "Debug: %s\n", orig_line.toLocal8Bit().data() );
|
|---|
| 66 | #endif
|
|---|
| 67 | line2 = orig_line;
|
|---|
| 68 | }
|
|---|
| 69 | break;
|
|---|
| 70 | case QtWarningMsg:
|
|---|
| 71 | #ifndef NO_DEBUG_ON_CONSOLE
|
|---|
| 72 | fprintf( stderr, "Warning: %s\n", orig_line.toLocal8Bit().data() );
|
|---|
| 73 | #endif
|
|---|
| 74 | line2 = "WARNING: " + orig_line;
|
|---|
| 75 | break;
|
|---|
| 76 | case QtFatalMsg:
|
|---|
| 77 | #ifndef NO_DEBUG_ON_CONSOLE
|
|---|
| 78 | fprintf( stderr, "Fatal: %s\n", orig_line.toLocal8Bit().data() );
|
|---|
| 79 | #endif
|
|---|
| 80 | line2 = "FATAL: " + orig_line;
|
|---|
| 81 | abort(); // deliberately core dump
|
|---|
| 82 | case QtCriticalMsg:
|
|---|
| 83 | #ifndef NO_DEBUG_ON_CONSOLE
|
|---|
| 84 | fprintf( stderr, "Critical: %s\n", orig_line.toLocal8Bit().data() );
|
|---|
| 85 | #endif
|
|---|
| 86 | line2 = "CRITICAL: " + orig_line;
|
|---|
| 87 | break;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | if (line2.isEmpty()) return;
|
|---|
| 91 |
|
|---|
| 92 | line2 = "["+ QTime::currentTime().toString("hh:mm:ss:zzz") +"] "+ line2;
|
|---|
| 93 |
|
|---|
| 94 | if (basegui_instance) {
|
|---|
| 95 | if (!saved_lines.isEmpty()) {
|
|---|
| 96 | // Send saved lines first
|
|---|
| 97 | for (int n=0; n < saved_lines.count(); n++) {
|
|---|
| 98 | basegui_instance->recordSmplayerLog(saved_lines[n]);
|
|---|
| 99 | }
|
|---|
| 100 | saved_lines.clear();
|
|---|
| 101 | }
|
|---|
| 102 | basegui_instance->recordSmplayerLog(line2);
|
|---|
| 103 | } else {
|
|---|
| 104 | // GUI is not created yet, save lines for later
|
|---|
| 105 | saved_lines.append(line2);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if (pref) {
|
|---|
| 109 | if (pref->save_smplayer_log) {
|
|---|
| 110 | // Save log to file
|
|---|
| 111 | if (!output_log.isOpen()) {
|
|---|
| 112 | // FIXME: the config path may not be initialized if USE_LOCKS is not defined
|
|---|
| 113 | output_log.setFileName( Paths::configPath() + "/smplayer_log.txt" );
|
|---|
| 114 | output_log.open(QIODevice::WriteOnly);
|
|---|
| 115 | }
|
|---|
| 116 | if (output_log.isOpen()) {
|
|---|
| 117 | QString l = line2 + "\r\n";
|
|---|
| 118 | output_log.write(l.toUtf8().constData());
|
|---|
| 119 | output_log.flush();
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | class MyApplication : public QApplication
|
|---|
| 127 | {
|
|---|
| 128 | public:
|
|---|
| 129 | MyApplication ( int & argc, char ** argv ) : QApplication(argc, argv) {};
|
|---|
| 130 | virtual void commitData ( QSessionManager & /*manager*/ ) {
|
|---|
| 131 | // Nothing to do, let the application to close
|
|---|
| 132 | }
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | int main( int argc, char ** argv )
|
|---|
| 136 | {
|
|---|
| 137 | MyApplication a( argc, argv );
|
|---|
| 138 | a.setQuitOnLastWindowClosed(false);
|
|---|
| 139 | //a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
|
|---|
| 140 |
|
|---|
| 141 | #if QT_VERSION >= 0x040400
|
|---|
| 142 | // Enable icons in menus
|
|---|
| 143 | QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, false);
|
|---|
| 144 | #endif
|
|---|
| 145 |
|
|---|
| 146 | // Sets the config path
|
|---|
| 147 | QString config_path;
|
|---|
| 148 |
|
|---|
| 149 | #ifdef PORTABLE_APP
|
|---|
| 150 | config_path = a.applicationDirPath();
|
|---|
| 151 | #else
|
|---|
| 152 | // If a smplayer.ini exists in the app path, will use that path
|
|---|
| 153 | // for the config file by default
|
|---|
| 154 | if (QFile::exists( a.applicationDirPath() + "/smplayer.ini" ) ) {
|
|---|
| 155 | config_path = a.applicationDirPath();
|
|---|
| 156 | qDebug("main: using existing %s", QString(config_path + "/smplayer.ini").toUtf8().data());
|
|---|
| 157 | }
|
|---|
| 158 | #endif
|
|---|
| 159 |
|
|---|
| 160 | QStringList args = a.arguments();
|
|---|
| 161 | int pos = args.indexOf("-config-path");
|
|---|
| 162 | if ( pos != -1) {
|
|---|
| 163 | if (pos+1 < args.count()) {
|
|---|
| 164 | pos++;
|
|---|
| 165 | config_path = args[pos];
|
|---|
| 166 | // Delete from list
|
|---|
| 167 | args.removeAt(pos);
|
|---|
| 168 | args.removeAt(pos-1);
|
|---|
| 169 | } else {
|
|---|
| 170 | printf("Error: expected parameter for -config-path\r\n");
|
|---|
| 171 | return SMPlayer::ErrorArgument;
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | qInstallMsgHandler( myMessageOutput );
|
|---|
| 176 |
|
|---|
| 177 | #if USE_LOCKS
|
|---|
| 178 | //setIniPath will be set later in global_init, but we need it here
|
|---|
| 179 | if (!config_path.isEmpty()) Paths::setConfigPath(config_path);
|
|---|
| 180 |
|
|---|
| 181 | QString lock_file = Paths::iniPath() + "/smplayer_init.lock";
|
|---|
| 182 | qDebug("main: lock_file: %s", lock_file.toUtf8().data());
|
|---|
| 183 |
|
|---|
| 184 | QtLockedFile lk(lock_file);
|
|---|
| 185 | lk.open(QFile::ReadWrite);
|
|---|
| 186 |
|
|---|
| 187 | if (QDir().exists(Paths::iniPath())) {
|
|---|
| 188 | bool lock_ok = lk.lock(QtLockedFile::WriteLock, false);
|
|---|
| 189 |
|
|---|
| 190 | if (!lock_ok) {
|
|---|
| 191 | //lock failed
|
|---|
| 192 | qDebug("main: lock failed");
|
|---|
| 193 |
|
|---|
| 194 | // Wait 10 secs max.
|
|---|
| 195 | int n = 100;
|
|---|
| 196 | while ( n > 0) {
|
|---|
| 197 | Helper::msleep(100); // wait 100 ms
|
|---|
| 198 |
|
|---|
| 199 | if (lk.lock(QtLockedFile::WriteLock, false)) break;
|
|---|
| 200 | n--;
|
|---|
| 201 | if ((n % 10) == 0) qDebug("main: waiting %d...", n);
|
|---|
| 202 | }
|
|---|
| 203 | // Continue startup
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | #endif // USE_LOCKS
|
|---|
| 207 |
|
|---|
| 208 | SMPlayer * smplayer = new SMPlayer(config_path);
|
|---|
| 209 | SMPlayer::ExitCode c = smplayer->processArgs( args );
|
|---|
| 210 | if (c != SMPlayer::NoExit) {
|
|---|
| 211 | #if USE_LOCKS
|
|---|
| 212 | lk.unlock();
|
|---|
| 213 | #endif
|
|---|
| 214 | return c;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | basegui_instance = smplayer->gui();
|
|---|
| 218 | a.connect(smplayer->gui(), SIGNAL(quitSolicited()), &a, SLOT(quit()));
|
|---|
| 219 | smplayer->start();
|
|---|
| 220 |
|
|---|
| 221 | #if USE_LOCKS
|
|---|
| 222 | bool success = lk.unlock();
|
|---|
| 223 | qDebug("Unlocking: %d", success);
|
|---|
| 224 | #endif
|
|---|
| 225 |
|
|---|
| 226 | int r = a.exec();
|
|---|
| 227 |
|
|---|
| 228 | basegui_instance = 0;
|
|---|
| 229 | delete smplayer;
|
|---|
| 230 |
|
|---|
| 231 | if (output_log.isOpen()) output_log.close();
|
|---|
| 232 |
|
|---|
| 233 | return r;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|