source: smplayer/vendor/0.8.2/src/smplayer.cpp

Last change on this file was 137, checked in by Silvan Scherrer, 13 years ago

SMPlayer: updated vendor to 0.8.2

File size: 14.6 KB
Line 
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 "smplayer.h"
20#include "defaultgui.h"
21#include "minigui.h"
22#include "mpcgui.h"
23#include "global.h"
24#include "paths.h"
25#include "translator.h"
26#include "version.h"
27#include "config.h"
28#include "clhelp.h"
29#include "myapplication.h"
30
31#ifdef SKINS
32#include "skingui.h"
33#endif
34
35#include <QDir>
36#include <QUrl>
37#include <QTime>
38#include <stdio.h>
39
40#ifdef Q_OS_WIN
41#if USE_ASSOCIATIONS
42#include "extensions.h"
43#include "winfileassoc.h" //required for Uninstall
44#endif
45#endif
46
47#ifdef FONTCACHE_DIALOG
48#include "fontcache.h"
49#include "version.h"
50#endif
51
52using namespace Global;
53
54BaseGui * SMPlayer::main_window = 0;
55
56SMPlayer::SMPlayer(const QString & config_path, QObject * parent )
57 : QObject(parent)
58{
59#ifdef LOG_SMPLAYER
60 qInstallMsgHandler( SMPlayer::myMessageOutput );
61 allow_to_send_log_to_gui = true;
62#endif
63
64 gui_to_use = "DefaultGui";
65
66 close_at_end = -1; // Not set
67 start_in_fullscreen = -1; // Not set
68
69 move_gui = false;
70 resize_gui = false;
71
72 Paths::setAppPath( qApp->applicationDirPath() );
73
74#ifndef PORTABLE_APP
75 if (config_path.isEmpty()) createConfigDirectory();
76#endif
77 global_init(config_path);
78
79 // Application translations
80 translator->load( pref->language );
81 showInfo();
82}
83
84SMPlayer::~SMPlayer() {
85 if (main_window != 0) {
86 deleteGUI();
87 }
88 global_end();
89
90#ifdef LOG_SMPLAYER
91 if (output_log.isOpen()) output_log.close();
92#endif
93}
94
95BaseGui * SMPlayer::gui() {
96 if (main_window == 0) {
97 // Changes to app path, so smplayer can find a relative mplayer path
98 QDir::setCurrent(Paths::appPath());
99 qDebug("SMPlayer::gui: changed working directory to app path");
100 qDebug("SMPlayer::gui: current directory: %s", QDir::currentPath().toUtf8().data());
101
102 main_window = createGUI(gui_to_use);
103
104 if (move_gui) {
105 qDebug("SMPlayer::gui: moving main window to %d %d", gui_position.x(), gui_position.y());
106 main_window->move(gui_position);
107 }
108 if (resize_gui) {
109 qDebug("SMPlayer::gui: resizing main window to %dx%d", gui_size.width(), gui_size.height());
110 main_window->resize(gui_size);
111 }
112 }
113
114 return main_window;
115}
116
117BaseGui * SMPlayer::createGUI(QString gui_name) {
118 BaseGui * gui = 0;
119
120#ifdef SKINS
121 if (gui_name.toLower() == "skingui")
122 gui = new SkinGui(0);
123 else
124#endif
125 if (gui_name.toLower() == "minigui")
126 gui = new MiniGui(0);
127 else
128 if (gui_name.toLower() == "mpcgui")
129 gui = new MpcGui(0);
130 else
131 gui = new DefaultGui(0);
132
133 gui->setForceCloseOnFinish(close_at_end);
134 gui->setForceStartInFullscreen(start_in_fullscreen);
135 connect(gui, SIGNAL(quitSolicited()), qApp, SLOT(quit()));
136
137#ifdef GUI_CHANGE_ON_RUNTIME
138 connect(gui, SIGNAL(guiChanged(QString)), this, SLOT(changeGUI(QString)));
139#endif
140
141#if SINGLE_INSTANCE
142 MyApplication * app = MyApplication::instance();
143 connect(app, SIGNAL(messageReceived(const QString&)),
144 gui, SLOT(handleMessageFromOtherInstances(const QString&)));
145 app->setActivationWindow(gui);
146#endif
147
148 return gui;
149}
150
151void SMPlayer::deleteGUI() {
152#ifdef LOG_SMPLAYER
153 allow_to_send_log_to_gui = false;
154#endif
155
156 delete main_window;
157 main_window = 0;
158
159#ifdef LOG_SMPLAYER
160 allow_to_send_log_to_gui = true;
161#endif
162}
163
164#ifdef GUI_CHANGE_ON_RUNTIME
165void SMPlayer::changeGUI(QString new_gui) {
166 qDebug("SMPlayer::changeGUI: '%s'", new_gui.toLatin1().constData());
167
168 deleteGUI();
169
170 main_window = createGUI(new_gui);
171
172 main_window->show();
173}
174#endif
175
176SMPlayer::ExitCode SMPlayer::processArgs(QStringList args) {
177 qDebug("SMPlayer::processArgs: arguments: %d", args.count());
178 for (int n = 0; n < args.count(); n++) {
179 qDebug("SMPlayer::processArgs: %d = %s", n, args[n].toUtf8().data());
180 }
181
182
183 QString action; // Action to be passed to running instance
184 bool show_help = false;
185
186 if (!pref->gui.isEmpty()) gui_to_use = pref->gui;
187 bool add_to_playlist = false;
188
189#ifdef Q_OS_WIN
190 if (args.contains("-uninstall")){
191#if USE_ASSOCIATIONS
192 //Called by uninstaller. Will restore old associations.
193 WinFileAssoc RegAssoc;
194 Extensions exts;
195 QStringList regExts;
196 RegAssoc.GetRegisteredExtensions(exts.multimedia(), regExts);
197 RegAssoc.RestoreFileAssociations(regExts);
198 printf("Restored associations\n");
199#endif
200 return NoError;
201 }
202#endif
203
204 for (int n = 1; n < args.count(); n++) {
205 QString argument = args[n];
206
207 if (argument == "-send-action") {
208 if (n+1 < args.count()) {
209 n++;
210 action = args[n];
211 } else {
212 printf("Error: expected parameter for -send-action\r\n");
213 return ErrorArgument;
214 }
215 }
216 else
217 if (argument == "-actions") {
218 if (n+1 < args.count()) {
219 n++;
220 actions_list = args[n];
221 } else {
222 printf("Error: expected parameter for -actions\r\n");
223 return ErrorArgument;
224 }
225 }
226 else
227 if (argument == "-sub") {
228 if (n+1 < args.count()) {
229 n++;
230 QString file = args[n];
231 if (QFile::exists(file)) {
232 subtitle_file = QFileInfo(file).absoluteFilePath();
233 } else {
234 printf("Error: file '%s' doesn't exists\r\n", file.toUtf8().constData());
235 }
236 } else {
237 printf("Error: expected parameter for -sub\r\n");
238 return ErrorArgument;
239 }
240 }
241 else
242 if (argument == "-pos") {
243 if (n+2 < args.count()) {
244 bool ok_x, ok_y;
245 n++;
246 gui_position.setX( args[n].toInt(&ok_x) );
247 n++;
248 gui_position.setY( args[n].toInt(&ok_y) );
249 if (ok_x && ok_y) move_gui = true;
250 } else {
251 printf("Error: expected parameter for -pos\r\n");
252 return ErrorArgument;
253 }
254 }
255 else
256 if (argument == "-size") {
257 if (n+2 < args.count()) {
258 bool ok_width, ok_height;
259 n++;
260 gui_size.setWidth( args[n].toInt(&ok_width) );
261 n++;
262 gui_size.setHeight( args[n].toInt(&ok_height) );
263 if (ok_width && ok_height) resize_gui = true;
264 } else {
265 printf("Error: expected parameter for -resize\r\n");
266 return ErrorArgument;
267 }
268 }
269 else
270 if ((argument == "--help") || (argument == "-help") ||
271 (argument == "-h") || (argument == "-?") )
272 {
273 show_help = true;
274 }
275 else
276 if (argument == "-close-at-end") {
277 close_at_end = 1;
278 }
279 else
280 if (argument == "-no-close-at-end") {
281 close_at_end = 0;
282 }
283 else
284 if (argument == "-fullscreen") {
285 start_in_fullscreen = 1;
286 }
287 else
288 if (argument == "-no-fullscreen") {
289 start_in_fullscreen = 0;
290 }
291 else
292 if (argument == "-add-to-playlist") {
293 add_to_playlist = true;
294 }
295 else
296 if (argument == "-mini" || argument == "-minigui") {
297 gui_to_use = "MiniGui";
298 }
299 else
300 if (argument == "-mpcgui") {
301 gui_to_use = "MpcGui";
302 }
303 else
304 if (argument == "-defaultgui") {
305 gui_to_use = "DefaultGui";
306 }
307 else {
308 // File
309 #if QT_VERSION >= 0x040600
310 QUrl fUrl = QUrl::fromUserInput(argument);
311 if (fUrl.isValid() && fUrl.scheme().toLower() == "file") {
312 argument = fUrl.toLocalFile();
313 }
314 #endif
315 if (QFile::exists( argument )) {
316 argument = QFileInfo(argument).absoluteFilePath();
317 }
318 files_to_play.append( argument );
319 }
320 }
321
322 if (show_help) {
323 printf("%s\n", CLHelp::help().toLocal8Bit().data());
324 return NoError;
325 }
326
327 qDebug("SMPlayer::processArgs: files_to_play: count: %d", files_to_play.count() );
328 for (int n=0; n < files_to_play.count(); n++) {
329 qDebug("SMPlayer::processArgs: files_to_play[%d]: '%s'", n, files_to_play[n].toUtf8().data());
330 }
331
332#ifdef SINGLE_INSTANCE
333 if (pref->use_single_instance) {
334 // Single instance
335 MyApplication * a = MyApplication::instance();
336 if (a->isRunning()) {
337 a->sendMessage("Hello");
338
339 if (!action.isEmpty()) {
340 a->sendMessage("action " + action);
341 }
342 else {
343 if (!subtitle_file.isEmpty()) {
344 a->sendMessage("load_sub " + subtitle_file);
345 }
346
347 if (!files_to_play.isEmpty()) {
348 /* a->sendMessage("open_file " + files_to_play[0]); */
349 QString command = "open_files";
350 if (add_to_playlist) command = "add_to_playlist";
351 a->sendMessage(command +" "+ files_to_play.join(" <<sep>> "));
352 }
353 }
354
355 return NoError;
356 }
357 }
358#endif
359
360 if (!pref->default_font.isEmpty()) {
361 QFont f;
362 f.fromString(pref->default_font);
363 qApp->setFont(f);
364 }
365
366 return SMPlayer::NoExit;
367}
368
369void SMPlayer::start() {
370#ifdef FONTCACHE_DIALOG
371#ifndef PORTABLE_APP
372 if (smplayerVersion() != pref->smplayer_version) {
373 FontCacheDialog d(0);
374 d.run(pref->mplayer_bin, "sample.avi");
375 pref->smplayer_version = smplayerVersion();
376 }
377#endif
378#endif
379
380 if (!gui()->startHidden() || !files_to_play.isEmpty() ) gui()->show();
381 if (!files_to_play.isEmpty()) {
382 if (!subtitle_file.isEmpty()) gui()->setInitialSubtitle(subtitle_file);
383 gui()->openFiles(files_to_play);
384 }
385
386 if (!actions_list.isEmpty()) {
387 if (files_to_play.isEmpty()) {
388 gui()->runActions(actions_list);
389 } else {
390 gui()->runActionsLater(actions_list);
391 }
392 }
393}
394
395#ifndef PORTABLE_APP
396void SMPlayer::createConfigDirectory() {
397 // Create smplayer config directory
398 if (!QFile::exists(Paths::configPath())) {
399 QDir d;
400 if (!d.mkdir(Paths::configPath())) {
401 qWarning("SMPlayer::createConfigDirectory: can't create %s", Paths::configPath().toUtf8().data());
402 }
403 // Screenshot folder already created in preferences.cpp if Qt >= 4.4
404 #if QT_VERSION < 0x040400
405 QString s = Paths::configPath() + "/screenshots";
406 if (!d.mkdir(s)) {
407 qWarning("SMPlayer::createHomeDirectory: can't create %s", s.toUtf8().data());
408 }
409 #endif
410 }
411}
412#endif
413
414void SMPlayer::showInfo() {
415#ifdef Q_OS_WIN
416 QString win_ver;
417 switch (QSysInfo::WindowsVersion) {
418 case QSysInfo::WV_2000: win_ver = "Windows 2000"; break;
419 case QSysInfo::WV_XP: win_ver = "Windows XP"; break;
420 case QSysInfo::WV_2003: win_ver = "Windows XP Professional x64/Server 2003"; break;
421 case QSysInfo::WV_VISTA: win_ver = "Windows Vista/Server 2008"; break;
422 #if QT_VERSION >= 0x040501
423 case QSysInfo::WV_WINDOWS7: win_ver = "Windows 7/Server 2008 R2"; break;
424 #endif
425 #if QT_VERSION >= 0x040803
426 case QSysInfo::WV_WINDOWS8: win_ver = "Windows 8/Server 2012"; break;
427 #endif
428 case QSysInfo::WV_NT_based: win_ver = "NT-based Windows"; break;
429 default: win_ver = QString("Unknown/Unsupported Windows OS"); break;
430 }
431#endif
432 QString s = QObject::tr("This is SMPlayer v. %1 running on %2")
433 .arg(smplayerVersion())
434#ifdef Q_OS_LINUX
435 .arg("Linux")
436#else
437#ifdef Q_OS_WIN
438 .arg("Windows ("+win_ver+")")
439#else
440#ifdef Q_OS_OS2
441 .arg("eCS (OS/2)")
442#else
443 .arg("Other OS")
444#endif
445#endif
446#endif
447 ;
448
449 printf("%s\n", s.toLocal8Bit().data() );
450 qDebug("%s", s.toUtf8().data() );
451 qDebug("Compiled with Qt v. %s, using %s", QT_VERSION_STR, qVersion());
452
453 qDebug(" * application path: '%s'", Paths::appPath().toUtf8().data());
454 qDebug(" * data path: '%s'", Paths::dataPath().toUtf8().data());
455 qDebug(" * translation path: '%s'", Paths::translationPath().toUtf8().data());
456 qDebug(" * doc path: '%s'", Paths::docPath().toUtf8().data());
457 qDebug(" * themes path: '%s'", Paths::themesPath().toUtf8().data());
458 qDebug(" * shortcuts path: '%s'", Paths::shortcutsPath().toUtf8().data());
459 qDebug(" * config path: '%s'", Paths::configPath().toUtf8().data());
460 qDebug(" * ini path: '%s'", Paths::iniPath().toUtf8().data());
461 qDebug(" * file for subtitles' styles: '%s'", Paths::subtitleStyleFile().toUtf8().data());
462 qDebug(" * current path: '%s'", QDir::currentPath().toUtf8().data());
463}
464
465#ifdef LOG_SMPLAYER
466QFile SMPlayer::output_log;
467bool SMPlayer::allow_to_send_log_to_gui = false;
468
469void SMPlayer::myMessageOutput( QtMsgType type, const char *msg ) {
470 static QStringList saved_lines;
471 static QString orig_line;
472 static QString line2;
473 static QRegExp rx_log;
474
475 if (pref) {
476 if (!pref->log_smplayer) return;
477 rx_log.setPattern(pref->log_filter);
478 } else {
479 rx_log.setPattern(".*");
480 }
481
482 line2.clear();
483
484 orig_line = QString::fromUtf8(msg);
485
486 switch ( type ) {
487 case QtDebugMsg:
488 if (rx_log.indexIn(orig_line) > -1) {
489 #ifndef NO_DEBUG_ON_CONSOLE
490 fprintf( stderr, "Debug: %s\n", orig_line.toLocal8Bit().data() );
491 #endif
492 line2 = orig_line;
493 }
494 break;
495 case QtWarningMsg:
496 #ifndef NO_DEBUG_ON_CONSOLE
497 fprintf( stderr, "Warning: %s\n", orig_line.toLocal8Bit().data() );
498 #endif
499 line2 = "WARNING: " + orig_line;
500 break;
501 case QtFatalMsg:
502 #ifndef NO_DEBUG_ON_CONSOLE
503 fprintf( stderr, "Fatal: %s\n", orig_line.toLocal8Bit().data() );
504 #endif
505 line2 = "FATAL: " + orig_line;
506 abort(); // deliberately core dump
507 case QtCriticalMsg:
508 #ifndef NO_DEBUG_ON_CONSOLE
509 fprintf( stderr, "Critical: %s\n", orig_line.toLocal8Bit().data() );
510 #endif
511 line2 = "CRITICAL: " + orig_line;
512 break;
513 }
514
515 if (line2.isEmpty()) return;
516
517 line2 = "["+ QTime::currentTime().toString("hh:mm:ss:zzz") +"] "+ line2;
518
519 if (allow_to_send_log_to_gui && main_window) {
520 if (!saved_lines.isEmpty()) {
521 // Send saved lines first
522 for (int n=0; n < saved_lines.count(); n++) {
523 main_window->recordSmplayerLog(saved_lines[n]);
524 }
525 saved_lines.clear();
526 }
527 main_window->recordSmplayerLog(line2);
528 } else {
529 // GUI is not created yet, save lines for later
530 saved_lines.append(line2);
531 /* printf("SMPlayer::myMessageOutput: no gui\n"); */
532 }
533
534 if (pref) {
535 if (pref->save_smplayer_log) {
536 // Save log to file
537 if (!output_log.isOpen()) {
538 // FIXME: the config path may not be initialized if USE_LOCKS is not defined
539 output_log.setFileName( Paths::configPath() + "/smplayer_log.txt" );
540 output_log.open(QIODevice::WriteOnly);
541 }
542 if (output_log.isOpen()) {
543 QString l = line2 + "\r\n";
544 output_log.write(l.toUtf8().constData());
545 output_log.flush();
546 }
547 }
548 }
549}
550#endif
551
552/*
553void myMessageOutput( QtMsgType type, const char *msg ) {
554 static QString orig_line;
555 orig_line = QString::fromUtf8(msg);
556
557 switch ( type ) {
558 case QtDebugMsg:
559 #ifndef NO_DEBUG_ON_CONSOLE
560 fprintf( stderr, "Debug: %s\n", orig_line.toLocal8Bit().data() );
561 #endif
562 break;
563
564 case QtWarningMsg:
565 #ifndef NO_DEBUG_ON_CONSOLE
566 fprintf( stderr, "Warning: %s\n", orig_line.toLocal8Bit().data() );
567 #endif
568 break;
569
570 case QtCriticalMsg:
571 #ifndef NO_DEBUG_ON_CONSOLE
572 fprintf( stderr, "Critical: %s\n", orig_line.toLocal8Bit().data() );
573 #endif
574 break;
575
576 case QtFatalMsg:
577 #ifndef NO_DEBUG_ON_CONSOLE
578 fprintf( stderr, "Fatal: %s\n", orig_line.toLocal8Bit().data() );
579 #endif
580 abort(); // deliberately core dump
581 }
582}
583*/
584
585#include "moc_smplayer.cpp"
Note: See TracBrowser for help on using the repository browser.