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

Last change on this file since 174 was 170, checked in by Silvan Scherrer, 11 years ago

SMPlayer: updated trunk to 14.9.0

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