source: smplayer/trunk/src/main.cpp@ 121

Last change on this file since 121 was 119, checked in by Silvan Scherrer, 14 years ago

SMPlayer: latest svn update

  • Property svn:eol-style set to LF
File size: 7.1 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2011 Ricardo Villalba <rvm@escomposlinux.org>
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
23#include "smplayer.h"
24#include "global.h"
25#include "helper.h"
26#include "paths.h"
27
28#include <stdio.h>
29
30#define USE_LOCKS 1
31#if USE_LOCKS
32#ifdef USE_QXT
33#define USE_QXT_LOCKS 1
34#endif // USE_QXT
35#endif // USE_LOCKS
36
37#if USE_LOCKS && USE_QXT_LOCKS
38#include <QxtFileLock>
39#endif
40
41using namespace Global;
42
43BaseGui * basegui_instance = 0;
44
45QFile output_log;
46
47void myMessageOutput( QtMsgType type, const char *msg ) {
48 static QStringList saved_lines;
49 static QString orig_line;
50 static QString line2;
51 static QRegExp rx_log;
52
53 if (pref) {
54 if (!pref->log_smplayer) return;
55 rx_log.setPattern(pref->log_filter);
56 } else {
57 rx_log.setPattern(".*");
58 }
59
60 line2.clear();
61
62 orig_line = QString::fromUtf8(msg);
63
64 switch ( type ) {
65 case QtDebugMsg:
66 if (rx_log.indexIn(orig_line) > -1) {
67 #ifndef NO_DEBUG_ON_CONSOLE
68 fprintf( stderr, "Debug: %s\n", orig_line.toLocal8Bit().data() );
69 #endif
70 line2 = orig_line;
71 }
72 break;
73 case QtWarningMsg:
74 #ifndef NO_DEBUG_ON_CONSOLE
75 fprintf( stderr, "Warning: %s\n", orig_line.toLocal8Bit().data() );
76 #endif
77 line2 = "WARNING: " + orig_line;
78 break;
79 case QtFatalMsg:
80 #ifndef NO_DEBUG_ON_CONSOLE
81 fprintf( stderr, "Fatal: %s\n", orig_line.toLocal8Bit().data() );
82 #endif
83 line2 = "FATAL: " + orig_line;
84 abort(); // deliberately core dump
85 case QtCriticalMsg:
86 #ifndef NO_DEBUG_ON_CONSOLE
87 fprintf( stderr, "Critical: %s\n", orig_line.toLocal8Bit().data() );
88 #endif
89 line2 = "CRITICAL: " + orig_line;
90 break;
91 }
92
93 if (line2.isEmpty()) return;
94
95 line2 = "["+ QTime::currentTime().toString("hh:mm:ss:zzz") +"] "+ line2;
96
97 if (basegui_instance) {
98 if (!saved_lines.isEmpty()) {
99 // Send saved lines first
100 for (int n=0; n < saved_lines.count(); n++) {
101 basegui_instance->recordSmplayerLog(saved_lines[n]);
102 }
103 saved_lines.clear();
104 }
105 basegui_instance->recordSmplayerLog(line2);
106 } else {
107 // GUI is not created yet, save lines for later
108 saved_lines.append(line2);
109 }
110
111 if (pref) {
112 if (pref->save_smplayer_log) {
113 // Save log to file
114 if (!output_log.isOpen()) {
115 // FIXME: the config path may not be initialized if USE_LOCKS is not defined
116 output_log.setFileName( Paths::configPath() + "/smplayer_log.txt" );
117 output_log.open(QIODevice::WriteOnly);
118 }
119 if (output_log.isOpen()) {
120 QString l = line2 + "\r\n";
121 output_log.write(l.toUtf8().constData());
122 output_log.flush();
123 }
124 }
125 }
126}
127
128#if USE_LOCKS
129#if USE_QXT_LOCKS
130bool create_lock(QFile * f, QxtFileLock * lock) {
131 bool success = false;
132 if (f->open(QIODevice::ReadWrite)) {
133 if (lock->lock()) {
134 f->write("smplayer lock file");
135 success = true;
136 }
137 }
138 if (!success) f->close();
139 return success;
140}
141
142void clean_lock(QFile * f, QxtFileLock * lock) {
143 qDebug("main: clean_lock: %s", f->fileName().toUtf8().data());
144 lock->unlock();
145 f->close();
146 f->remove();
147}
148#else
149void remove_lock(QString lock_file) {
150 if (QFile::exists(lock_file)) {
151 qDebug("main: remove_lock: %s", lock_file.toUtf8().data());
152 QFile::remove(lock_file);
153 }
154}
155#endif
156#endif
157
158class MyApplication : public QApplication
159{
160public:
161 MyApplication ( int & argc, char ** argv ) : QApplication(argc, argv) {};
162 virtual void commitData ( QSessionManager & /*manager*/ ) {
163 // Nothing to do, let the application to close
164 }
165};
166
167int main( int argc, char ** argv )
168{
169 MyApplication a( argc, argv );
170 a.setQuitOnLastWindowClosed(false);
171 //a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
172
173#if QT_VERSION >= 0x040400
174 // Enable icons in menus
175 QCoreApplication::setAttribute(Qt::AA_DontShowIconsInMenus, false);
176#endif
177
178 // Sets the config path
179 QString config_path;
180
181#ifdef PORTABLE_APP
182 config_path = a.applicationDirPath();
183#else
184 // If a smplayer.ini exists in the app path, will use that path
185 // for the config file by default
186 if (QFile::exists( a.applicationDirPath() + "/smplayer.ini" ) ) {
187 config_path = a.applicationDirPath();
188 qDebug("main: using existing %s", QString(config_path + "/smplayer.ini").toUtf8().data());
189 }
190#endif
191
192 QStringList args = a.arguments();
193 int pos = args.indexOf("-config-path");
194 if ( pos != -1) {
195 if (pos+1 < args.count()) {
196 pos++;
197 config_path = args[pos];
198 // Delete from list
199 args.removeAt(pos);
200 args.removeAt(pos-1);
201 } else {
202 printf("Error: expected parameter for -config-path\r\n");
203 return SMPlayer::ErrorArgument;
204 }
205 }
206
207 qInstallMsgHandler( myMessageOutput );
208
209#if USE_LOCKS
210 //setIniPath will be set later in global_init, but we need it here
211 if (!config_path.isEmpty()) Paths::setConfigPath(config_path);
212
213 QString lock_file = Paths::iniPath() + "/smplayer_init.lock";
214 qDebug("main: lock_file: %s", lock_file.toUtf8().data());
215
216#if USE_QXT_LOCKS
217 QFile f(lock_file);
218 QxtFileLock write_lock(&f, 0x10, 30, QxtFileLock::WriteLock);
219
220 bool lock_ok = create_lock(&f, &write_lock);
221
222 if (!lock_ok) {
223 //lock failed
224 qDebug("main: lock failed");
225
226 // Wait 10 secs max.
227 int n = 100;
228 while ( n > 0) {
229 Helper::msleep(100); // wait 100 ms
230 //if (!QFile::exists(lock_file)) break;
231 if (create_lock(&f, &write_lock)) break;
232 n--;
233 if ((n % 10) == 0) qDebug("main: waiting %d...", n);
234 }
235 // Continue startup
236 }
237#else
238 if (QFile::exists(lock_file)) {
239 qDebug("main: %s exists, waiting...", lock_file.toUtf8().data());
240 // Wait 10 secs max.
241 int n = 100;
242 while ( n > 0) {
243 Helper::msleep(100); // wait 100 ms
244 if (!QFile::exists(lock_file)) break;
245 n--;
246 if ((n % 10) == 0) qDebug("main: waiting %d...", n);
247 }
248 remove_lock(lock_file);
249 } else {
250 // Create lock file
251 QFile f(lock_file);
252 if (f.open(QIODevice::WriteOnly)) {
253 f.write("smplayer lock file");
254 f.close();
255 } else {
256 qWarning("main: can't open %s for writing", lock_file.toUtf8().data());
257 }
258
259 }
260#endif // USE_QXT_LOCKS
261#endif // USE_LOCKS
262
263 SMPlayer * smplayer = new SMPlayer(config_path);
264 SMPlayer::ExitCode c = smplayer->processArgs( args );
265 if (c != SMPlayer::NoExit) {
266#if USE_LOCKS
267#if USE_QXT_LOCKS
268 clean_lock(&f, &write_lock);
269#else
270 remove_lock(lock_file);
271#endif
272#endif
273 return c;
274 }
275
276 basegui_instance = smplayer->gui();
277 a.connect(smplayer->gui(), SIGNAL(quitSolicited()), &a, SLOT(quit()));
278 smplayer->start();
279
280#if USE_LOCKS
281#if USE_QXT_LOCKS
282 clean_lock(&f, &write_lock);
283#else
284 remove_lock(lock_file);
285#endif
286#endif
287
288 int r = a.exec();
289
290 basegui_instance = 0;
291 delete smplayer;
292
293 if (output_log.isOpen()) output_log.close();
294
295 return r;
296}
297
Note: See TracBrowser for help on using the repository browser.