source: smplayer/trunk/src/smplayer.cpp@ 148

Last change on this file since 148 was 142, checked in by Silvan Scherrer, 12 years ago

SMPlayer: update trunk to 0.8.5

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