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

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

SMPlayer: update trunk to 0.8.6

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