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

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

SMPlayer: 0.7.1 trunk update

  • Property svn:eol-style set to LF
File size: 11.2 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 "myclient.h"
29#include "clhelp.h"
30
31#include <QDir>
32#include <QApplication>
33#include <QUrl>
34
35#include <stdio.h>
36
37#ifdef Q_OS_WIN
38#if USE_ASSOCIATIONS
39#include "extensions.h"
40#include "winfileassoc.h" //required for Uninstall
41#endif
42#endif
43
44
45using namespace Global;
46
47SMPlayer::SMPlayer(const QString & config_path, QObject * parent )
48 : QObject(parent)
49{
50 main_window = 0;
51 gui_to_use = "DefaultGui";
52
53 close_at_end = -1; // Not set
54 start_in_fullscreen = -1; // Not set
55 use_control_server = true;
56
57 move_gui = false;
58 resize_gui = false;
59
60 Paths::setAppPath( qApp->applicationDirPath() );
61
62#ifndef PORTABLE_APP
63 if (config_path.isEmpty()) createConfigDirectory();
64#endif
65 global_init(config_path);
66
67 // Application translations
68 translator->load( pref->language );
69 showInfo();
70}
71
72SMPlayer::~SMPlayer() {
73 if (main_window != 0) delete main_window;
74 global_end();
75}
76
77BaseGui * SMPlayer::gui() {
78 if (main_window == 0) {
79 // Changes to app path, so smplayer can find a relative mplayer path
80 QDir::setCurrent(Paths::appPath());
81 qDebug("SMPlayer::gui: changed working directory to app path");
82 qDebug("SMPlayer::gui: current directory: %s", QDir::currentPath().toUtf8().data());
83
84 if (gui_to_use.toLower() == "minigui")
85 main_window = new MiniGui(use_control_server, 0);
86 else
87 if (gui_to_use.toLower() == "mpcgui")
88 main_window = new MpcGui(use_control_server, 0);
89 else
90 main_window = new DefaultGui(use_control_server, 0);
91
92 if (move_gui) {
93 qDebug("SMPlayer::gui: moving main window to %d %d", gui_position.x(), gui_position.y());
94 main_window->move(gui_position);
95 }
96 if (resize_gui) {
97 qDebug("SMPlayer::gui: resizing main window to %dx%d", gui_size.width(), gui_size.height());
98 main_window->resize(gui_size);
99 }
100
101 main_window->setForceCloseOnFinish(close_at_end);
102 main_window->setForceStartInFullscreen(start_in_fullscreen);
103 }
104
105 return main_window;
106}
107
108SMPlayer::ExitCode SMPlayer::processArgs(QStringList args) {
109 qDebug("SMPlayer::processArgs: arguments: %d", args.count());
110 for (int n = 0; n < args.count(); n++) {
111 qDebug("SMPlayer::processArgs: %d = %s", n, args[n].toUtf8().data());
112 }
113
114
115 QString action; // Action to be passed to running instance
116 bool show_help = false;
117
118 if (!pref->gui.isEmpty()) gui_to_use = pref->gui;
119 bool add_to_playlist = false;
120
121#ifdef Q_OS_WIN
122 if (args.contains("-uninstall")){
123#if USE_ASSOCIATIONS
124 //Called by uninstaller. Will restore old associations.
125 WinFileAssoc RegAssoc;
126 Extensions exts;
127 QStringList regExts;
128 RegAssoc.GetRegisteredExtensions(exts.multimedia(), regExts);
129 RegAssoc.RestoreFileAssociations(regExts);
130 printf("Restored associations\n");
131#endif
132 return NoError;
133 }
134#endif
135
136 for (int n = 1; n < args.count(); n++) {
137 QString argument = args[n];
138
139 if (argument == "-send-action") {
140 if (n+1 < args.count()) {
141 n++;
142 action = args[n];
143 } else {
144 printf("Error: expected parameter for -send-action\r\n");
145 return ErrorArgument;
146 }
147 }
148 else
149 if (argument == "-actions") {
150 if (n+1 < args.count()) {
151 n++;
152 actions_list = args[n];
153 } else {
154 printf("Error: expected parameter for -actions\r\n");
155 return ErrorArgument;
156 }
157 }
158 else
159 if (argument == "-sub") {
160 if (n+1 < args.count()) {
161 n++;
162 QString file = args[n];
163 if (QFile::exists(file)) {
164 subtitle_file = QFileInfo(file).absoluteFilePath();
165 } else {
166 printf("Error: file '%s' doesn't exists\r\n", file.toUtf8().constData());
167 }
168 } else {
169 printf("Error: expected parameter for -sub\r\n");
170 return ErrorArgument;
171 }
172 }
173 else
174 if (argument == "-pos") {
175 if (n+2 < args.count()) {
176 bool ok_x, ok_y;
177 n++;
178 gui_position.setX( args[n].toInt(&ok_x) );
179 n++;
180 gui_position.setY( args[n].toInt(&ok_y) );
181 if (ok_x && ok_y) move_gui = true;
182 } else {
183 printf("Error: expected parameter for -pos\r\n");
184 return ErrorArgument;
185 }
186 }
187 else
188 if (argument == "-size") {
189 if (n+2 < args.count()) {
190 bool ok_width, ok_height;
191 n++;
192 gui_size.setWidth( args[n].toInt(&ok_width) );
193 n++;
194 gui_size.setHeight( args[n].toInt(&ok_height) );
195 if (ok_width && ok_height) resize_gui = true;
196 } else {
197 printf("Error: expected parameter for -resize\r\n");
198 return ErrorArgument;
199 }
200 }
201 else
202 if ((argument == "--help") || (argument == "-help") ||
203 (argument == "-h") || (argument == "-?") )
204 {
205 show_help = true;
206 }
207 else
208 if (argument == "-close-at-end") {
209 close_at_end = 1;
210 }
211 else
212 if (argument == "-no-close-at-end") {
213 close_at_end = 0;
214 }
215 else
216 if (argument == "-fullscreen") {
217 start_in_fullscreen = 1;
218 }
219 else
220 if (argument == "-no-fullscreen") {
221 start_in_fullscreen = 0;
222 }
223 else
224 if (argument == "-disable-server") {
225 use_control_server = false;
226 }
227 else
228 if (argument == "-add-to-playlist") {
229 add_to_playlist = true;
230 }
231 else
232 if (argument == "-mini" || argument == "-minigui") {
233 gui_to_use = "MiniGui";
234 }
235 else
236 if (argument == "-mpcgui") {
237 gui_to_use = "MpcGui";
238 }
239 else
240 if (argument == "-defaultgui") {
241 gui_to_use = "DefaultGui";
242 }
243 else {
244 // File
245 #if QT_VERSION >= 0x040600
246 QUrl fUrl = QUrl::fromUserInput(argument);
247 if (fUrl.isValid() && fUrl.scheme().toLower() == "file") {
248 argument = fUrl.toLocalFile();
249 }
250 #endif
251 if (QFile::exists( argument )) {
252 argument = QFileInfo(argument).absoluteFilePath();
253 }
254 files_to_play.append( argument );
255 }
256 }
257
258 if (show_help) {
259 printf("%s\n", CLHelp::help().toLocal8Bit().data());
260 return NoError;
261 }
262
263 qDebug("SMPlayer::processArgs: files_to_play: count: %d", files_to_play.count() );
264 for (int n=0; n < files_to_play.count(); n++) {
265 qDebug("SMPlayer::processArgs: files_to_play[%d]: '%s'", n, files_to_play[n].toUtf8().data());
266 }
267
268
269 if (pref->use_single_instance) {
270 // Single instance
271 int port = pref->connection_port;
272 if (pref->use_autoport) port = pref->autoport;
273
274 MyClient *c = new MyClient(port);
275 //c->setTimeOut(1000);
276 qDebug("SMPlayer::processArgs: trying to connect to port %d", port);
277
278 if (c->openConnection()) {
279 qDebug("SMPlayer::processArgs: found another instance");
280
281 if (!action.isEmpty()) {
282 if (c->sendAction(action)) {
283 qDebug("SMPlayer::processArgs: action passed successfully to the running instance");
284 } else {
285 printf("Error: action couldn't be passed to the running instance");
286 return NoAction;
287 }
288 }
289 else {
290 if (!subtitle_file.isEmpty()) {
291 if (c->sendSubtitleFile(subtitle_file)) {
292 qDebug("SMPlayer::processArgs: subtitle file sent successfully to the running instance");
293 } else {
294 qDebug("SMPlayer::processArgs: subtitle file couldn't be sent to another instance");
295 }
296 }
297
298 if (!files_to_play.isEmpty()) {
299 if (c->sendFiles(files_to_play, add_to_playlist)) {
300 qDebug("SMPlayer::processArgs: files sent successfully to the running instance");
301 qDebug("SMPlayer::processArgs: exiting.");
302 } else {
303 qDebug("SMPlayer::processArgs: files couldn't be sent to another instance");
304 }
305 }
306 }
307 c->closeConnection();
308 return NoError;
309 } else {
310 if (!action.isEmpty()) {
311 printf("Error: no running instance found\r\n");
312 return NoRunningInstance;
313 }
314 }
315 }
316
317 if (!pref->default_font.isEmpty()) {
318 QFont f;
319 f.fromString(pref->default_font);
320 qApp->setFont(f);
321 }
322
323 return SMPlayer::NoExit;
324}
325
326void SMPlayer::start() {
327 if (!gui()->startHidden() || !files_to_play.isEmpty() ) gui()->show();
328 if (!files_to_play.isEmpty()) {
329 if (!subtitle_file.isEmpty()) gui()->setInitialSubtitle(subtitle_file);
330 gui()->openFiles(files_to_play);
331 }
332
333 if (!actions_list.isEmpty()) {
334 if (files_to_play.isEmpty()) {
335 gui()->runActions(actions_list);
336 } else {
337 gui()->runActionsLater(actions_list);
338 }
339 }
340}
341
342#ifndef PORTABLE_APP
343void SMPlayer::createConfigDirectory() {
344 // Create smplayer config directory
345 if (!QFile::exists(Paths::configPath())) {
346 QDir d;
347 if (!d.mkdir(Paths::configPath())) {
348 qWarning("SMPlayer::createConfigDirectory: can't create %s", Paths::configPath().toUtf8().data());
349 }
350 // Screenshot folder already created in preferences.cpp if Qt >= 4.4
351 #if QT_VERSION < 0x040400
352 QString s = Paths::configPath() + "/screenshots";
353 if (!d.mkdir(s)) {
354 qWarning("SMPlayer::createHomeDirectory: can't create %s", s.toUtf8().data());
355 }
356 #endif
357 }
358}
359#endif
360
361void SMPlayer::showInfo() {
362#ifdef Q_OS_WIN
363 QString win_ver;
364 switch (QSysInfo::WindowsVersion) {
365 case QSysInfo::WV_32s: win_ver = "Windows 3.1"; break;
366 case QSysInfo::WV_95: win_ver = "Windows 95"; break;
367 case QSysInfo::WV_98: win_ver = "Windows 98"; break;
368 case QSysInfo::WV_Me: win_ver = "Windows Me"; break;
369 case QSysInfo::WV_NT: win_ver = "Windows NT"; break;
370 case QSysInfo::WV_2000: win_ver = "Windows 2000"; break;
371 case QSysInfo::WV_XP: win_ver = "Windows XP"; break;
372 case QSysInfo::WV_2003: win_ver = "Windows Server 2003"; break;
373 case QSysInfo::WV_VISTA: win_ver = "Windows Vista"; break;
374 #if QT_VERSION >= 0x040501
375 case QSysInfo::WV_WINDOWS7: win_ver = "Windows 7"; break;
376 #endif
377 default: win_ver = QString("other: %1").arg(QSysInfo::WindowsVersion);
378 }
379#endif
380 QString s = QObject::tr("This is SMPlayer v. %1 running on %2")
381 .arg(smplayerVersion())
382#ifdef Q_OS_LINUX
383 .arg("Linux")
384#else
385#ifdef Q_OS_WIN
386 .arg("Windows ("+win_ver+")")
387#else
388#ifdef Q_OS_OS2
389 .arg("eCS (OS/2)")
390#else
391 .arg("Other OS")
392#endif
393#endif
394#endif
395 ;
396
397 printf("%s\n", s.toLocal8Bit().data() );
398 qDebug("%s", s.toUtf8().data() );
399 qDebug("Compiled with Qt v. %s, using %s", QT_VERSION_STR, qVersion());
400
401 qDebug(" * application path: '%s'", Paths::appPath().toUtf8().data());
402 qDebug(" * data path: '%s'", Paths::dataPath().toUtf8().data());
403 qDebug(" * translation path: '%s'", Paths::translationPath().toUtf8().data());
404 qDebug(" * doc path: '%s'", Paths::docPath().toUtf8().data());
405 qDebug(" * themes path: '%s'", Paths::themesPath().toUtf8().data());
406 qDebug(" * shortcuts path: '%s'", Paths::shortcutsPath().toUtf8().data());
407 qDebug(" * config path: '%s'", Paths::configPath().toUtf8().data());
408 qDebug(" * ini path: '%s'", Paths::iniPath().toUtf8().data());
409 qDebug(" * file for subtitles' styles: '%s'", Paths::subtitleStyleFile().toUtf8().data());
410 qDebug(" * current path: '%s'", QDir::currentPath().toUtf8().data());
411}
Note: See TracBrowser for help on using the repository browser.