| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2010 Ricardo Villalba <rvm@escomposlinux.org>
|
|---|
| 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 "constants.h"
|
|---|
| 28 | #include "myclient.h"
|
|---|
| 29 | #include "clhelp.h"
|
|---|
| 30 |
|
|---|
| 31 | #include <QDir>
|
|---|
| 32 | #include <QApplication>
|
|---|
| 33 |
|
|---|
| 34 | #include <stdio.h>
|
|---|
| 35 |
|
|---|
| 36 | #ifdef Q_OS_WIN
|
|---|
| 37 | #if USE_ASSOCIATIONS
|
|---|
| 38 | #include "extensions.h"
|
|---|
| 39 | #include "winfileassoc.h" //required for Uninstall
|
|---|
| 40 | #endif
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | using namespace Global;
|
|---|
| 45 |
|
|---|
| 46 | SMPlayer::SMPlayer(const QString & config_path, QObject * parent )
|
|---|
| 47 | : QObject(parent)
|
|---|
| 48 | {
|
|---|
| 49 | main_window = 0;
|
|---|
| 50 | gui_to_use = "DefaultGui";
|
|---|
| 51 |
|
|---|
| 52 | close_at_end = -1; // Not set
|
|---|
| 53 | start_in_fullscreen = -1; // Not set
|
|---|
| 54 |
|
|---|
| 55 | move_gui = false;
|
|---|
| 56 | resize_gui = false;
|
|---|
| 57 |
|
|---|
| 58 | Paths::setAppPath( qApp->applicationDirPath() );
|
|---|
| 59 |
|
|---|
| 60 | #ifndef PORTABLE_APP
|
|---|
| 61 | if (config_path.isEmpty()) createConfigDirectory();
|
|---|
| 62 | #endif
|
|---|
| 63 | global_init(config_path);
|
|---|
| 64 |
|
|---|
| 65 | // Application translations
|
|---|
| 66 | translator->load( pref->language );
|
|---|
| 67 | showInfo();
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | SMPlayer::~SMPlayer() {
|
|---|
| 71 | if (main_window != 0) delete main_window;
|
|---|
| 72 | global_end();
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | BaseGui * SMPlayer::gui() {
|
|---|
| 76 | if (main_window == 0) {
|
|---|
| 77 | // Changes to app path, so smplayer can find a relative mplayer path
|
|---|
| 78 | QDir::setCurrent(Paths::appPath());
|
|---|
| 79 | qDebug("SMPlayer::gui: changed working directory to app path");
|
|---|
| 80 | qDebug("SMPlayer::gui: current directory: %s", QDir::currentPath().toUtf8().data());
|
|---|
| 81 |
|
|---|
| 82 | if (gui_to_use.toLower() == "minigui")
|
|---|
| 83 | main_window = new MiniGui(0);
|
|---|
| 84 | else
|
|---|
| 85 | if (gui_to_use.toLower() == "mpcgui")
|
|---|
| 86 | main_window = new MpcGui(0);
|
|---|
| 87 | else
|
|---|
| 88 | main_window = new DefaultGui(0);
|
|---|
| 89 |
|
|---|
| 90 | if (move_gui) {
|
|---|
| 91 | qDebug("SMPlayer::gui: moving main window to %d %d", gui_position.x(), gui_position.y());
|
|---|
| 92 | main_window->move(gui_position);
|
|---|
| 93 | }
|
|---|
| 94 | if (resize_gui) {
|
|---|
| 95 | qDebug("SMPlayer::gui: resizing main window to %dx%d", gui_size.width(), gui_size.height());
|
|---|
| 96 | main_window->resize(gui_size);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | main_window->setForceCloseOnFinish(close_at_end);
|
|---|
| 100 | main_window->setForceStartInFullscreen(start_in_fullscreen);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return main_window;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | SMPlayer::ExitCode SMPlayer::processArgs(QStringList args) {
|
|---|
| 107 | qDebug("SMPlayer::processArgs: arguments: %d", args.count());
|
|---|
| 108 | for (int n = 0; n < args.count(); n++) {
|
|---|
| 109 | qDebug("SMPlayer::processArgs: %d = %s", n, args[n].toUtf8().data());
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | QString action; // Action to be passed to running instance
|
|---|
| 114 | bool show_help = false;
|
|---|
| 115 |
|
|---|
| 116 | if (!pref->gui.isEmpty()) gui_to_use = pref->gui;
|
|---|
| 117 | bool add_to_playlist = false;
|
|---|
| 118 |
|
|---|
| 119 | bool is_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 == "-playlist") {
|
|---|
| 203 | is_playlist = true;
|
|---|
| 204 | }
|
|---|
| 205 | else
|
|---|
| 206 | if ((argument == "--help") || (argument == "-help") ||
|
|---|
| 207 | (argument == "-h") || (argument == "-?") )
|
|---|
| 208 | {
|
|---|
| 209 | show_help = true;
|
|---|
| 210 | }
|
|---|
| 211 | else
|
|---|
| 212 | if (argument == "-close-at-end") {
|
|---|
| 213 | close_at_end = 1;
|
|---|
| 214 | }
|
|---|
| 215 | else
|
|---|
| 216 | if (argument == "-no-close-at-end") {
|
|---|
| 217 | close_at_end = 0;
|
|---|
| 218 | }
|
|---|
| 219 | else
|
|---|
| 220 | if (argument == "-fullscreen") {
|
|---|
| 221 | start_in_fullscreen = 1;
|
|---|
| 222 | }
|
|---|
| 223 | else
|
|---|
| 224 | if (argument == "-no-fullscreen") {
|
|---|
| 225 | start_in_fullscreen = 0;
|
|---|
| 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 (QFile::exists( argument )) {
|
|---|
| 246 | argument = QFileInfo(argument).absoluteFilePath();
|
|---|
| 247 | }
|
|---|
| 248 | if (is_playlist) {
|
|---|
| 249 | argument = argument + IS_PLAYLIST_TAG;
|
|---|
| 250 | is_playlist = false;
|
|---|
| 251 | }
|
|---|
| 252 | files_to_play.append( argument );
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | if (show_help) {
|
|---|
| 257 | printf("%s\n", CLHelp::help().toLocal8Bit().data());
|
|---|
| 258 | return NoError;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | qDebug("SMPlayer::processArgs: files_to_play: count: %d", files_to_play.count() );
|
|---|
| 262 | for (int n=0; n < files_to_play.count(); n++) {
|
|---|
| 263 | qDebug("SMPlayer::processArgs: files_to_play[%d]: '%s'", n, files_to_play[n].toUtf8().data());
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 | if (pref->use_single_instance) {
|
|---|
| 268 | // Single instance
|
|---|
| 269 | int port = pref->connection_port;
|
|---|
| 270 | if (pref->use_autoport) port = pref->autoport;
|
|---|
| 271 |
|
|---|
| 272 | MyClient *c = new MyClient(port);
|
|---|
| 273 | //c->setTimeOut(1000);
|
|---|
| 274 | qDebug("SMPlayer::processArgs: trying to connect to port %d", port);
|
|---|
| 275 |
|
|---|
| 276 | if (c->openConnection()) {
|
|---|
| 277 | qDebug("SMPlayer::processArgs: found another instance");
|
|---|
| 278 |
|
|---|
| 279 | if (!action.isEmpty()) {
|
|---|
| 280 | if (c->sendAction(action)) {
|
|---|
| 281 | qDebug("SMPlayer::processArgs: action passed successfully to the running instance");
|
|---|
| 282 | } else {
|
|---|
| 283 | printf("Error: action couldn't be passed to the running instance");
|
|---|
| 284 | return NoAction;
|
|---|
| 285 | }
|
|---|
| 286 | }
|
|---|
| 287 | else {
|
|---|
| 288 | if (!subtitle_file.isEmpty()) {
|
|---|
| 289 | if (c->sendSubtitleFile(subtitle_file)) {
|
|---|
| 290 | qDebug("SMPlayer::processArgs: subtitle file sent successfully to the running instance");
|
|---|
| 291 | } else {
|
|---|
| 292 | qDebug("SMPlayer::processArgs: subtitle file couldn't be sent to another instance");
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | if (!files_to_play.isEmpty()) {
|
|---|
| 297 | if (c->sendFiles(files_to_play, add_to_playlist)) {
|
|---|
| 298 | qDebug("SMPlayer::processArgs: files sent successfully to the running instance");
|
|---|
| 299 | qDebug("SMPlayer::processArgs: exiting.");
|
|---|
| 300 | } else {
|
|---|
| 301 | qDebug("SMPlayer::processArgs: files couldn't be sent to another instance");
|
|---|
| 302 | }
|
|---|
| 303 | }
|
|---|
| 304 | }
|
|---|
| 305 | c->closeConnection();
|
|---|
| 306 | return NoError;
|
|---|
| 307 | } else {
|
|---|
| 308 | if (!action.isEmpty()) {
|
|---|
| 309 | printf("Error: no running instance found\r\n");
|
|---|
| 310 | return NoRunningInstance;
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | if (!pref->default_font.isEmpty()) {
|
|---|
| 316 | QFont f;
|
|---|
| 317 | f.fromString(pref->default_font);
|
|---|
| 318 | qApp->setFont(f);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | return SMPlayer::NoExit;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | void SMPlayer::start() {
|
|---|
| 325 | if (!gui()->startHidden() || !files_to_play.isEmpty() ) gui()->show();
|
|---|
| 326 | if (!files_to_play.isEmpty()) {
|
|---|
| 327 | if (!subtitle_file.isEmpty()) gui()->setInitialSubtitle(subtitle_file);
|
|---|
| 328 | gui()->openFiles(files_to_play);
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | if (!actions_list.isEmpty()) {
|
|---|
| 332 | if (files_to_play.isEmpty()) {
|
|---|
| 333 | gui()->runActions(actions_list);
|
|---|
| 334 | } else {
|
|---|
| 335 | gui()->runActionsLater(actions_list);
|
|---|
| 336 | }
|
|---|
| 337 | }
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | #ifndef PORTABLE_APP
|
|---|
| 341 | void SMPlayer::createConfigDirectory() {
|
|---|
| 342 | // Create smplayer config directory
|
|---|
| 343 | if (!QFile::exists(Paths::configPath())) {
|
|---|
| 344 | QDir d;
|
|---|
| 345 | if (!d.mkdir(Paths::configPath())) {
|
|---|
| 346 | qWarning("SMPlayer::createConfigDirectory: can't create %s", Paths::configPath().toUtf8().data());
|
|---|
| 347 | }
|
|---|
| 348 | QString s = Paths::configPath() + "/screenshots";
|
|---|
| 349 | if (!d.mkdir(s)) {
|
|---|
| 350 | qWarning("SMPlayer::createHomeDirectory: can't create %s", s.toUtf8().data());
|
|---|
| 351 | }
|
|---|
| 352 | }
|
|---|
| 353 | }
|
|---|
| 354 | #endif
|
|---|
| 355 |
|
|---|
| 356 | void SMPlayer::showInfo() {
|
|---|
| 357 | #ifdef Q_OS_WIN
|
|---|
| 358 | QString win_ver;
|
|---|
| 359 | switch (QSysInfo::WindowsVersion) {
|
|---|
| 360 | case QSysInfo::WV_32s: win_ver = "Windows 3.1"; break;
|
|---|
| 361 | case QSysInfo::WV_95: win_ver = "Windows 95"; break;
|
|---|
| 362 | case QSysInfo::WV_98: win_ver = "Windows 98"; break;
|
|---|
| 363 | case QSysInfo::WV_Me: win_ver = "Windows Me"; break;
|
|---|
| 364 | case QSysInfo::WV_NT: win_ver = "Windows NT"; break;
|
|---|
| 365 | case QSysInfo::WV_2000: win_ver = "Windows 2000"; break;
|
|---|
| 366 | case QSysInfo::WV_XP: win_ver = "Windows XP"; break;
|
|---|
| 367 | case QSysInfo::WV_2003: win_ver = "Windows Server 2003"; break;
|
|---|
| 368 | case QSysInfo::WV_VISTA: win_ver = "Windows Vista"; break;
|
|---|
| 369 | #if QT_VERSION >= 0x040501
|
|---|
| 370 | case QSysInfo::WV_WINDOWS7: win_ver = "Windows 7"; break;
|
|---|
| 371 | #endif
|
|---|
| 372 | default: win_ver = QString("other: %1").arg(QSysInfo::WindowsVersion);
|
|---|
| 373 | }
|
|---|
| 374 | #endif
|
|---|
| 375 | QString s = QObject::tr("This is SMPlayer v. %1 running on %2")
|
|---|
| 376 | .arg(smplayerVersion())
|
|---|
| 377 | #ifdef Q_OS_LINUX
|
|---|
| 378 | .arg("Linux")
|
|---|
| 379 | #else
|
|---|
| 380 | #ifdef Q_OS_WIN
|
|---|
| 381 | .arg("Windows ("+win_ver+")")
|
|---|
| 382 | #else
|
|---|
| 383 | #ifdef Q_OS_OS2
|
|---|
| 384 | .arg("eCS (OS/2)")
|
|---|
| 385 | #else
|
|---|
| 386 | .arg("Other OS")
|
|---|
| 387 | #endif
|
|---|
| 388 | #endif
|
|---|
| 389 | #endif
|
|---|
| 390 | ;
|
|---|
| 391 |
|
|---|
| 392 | printf("%s\n", s.toLocal8Bit().data() );
|
|---|
| 393 | qDebug("%s", s.toUtf8().data() );
|
|---|
| 394 | qDebug("Compiled with Qt v. %s, using %s", QT_VERSION_STR, qVersion());
|
|---|
| 395 |
|
|---|
| 396 | qDebug(" * application path: '%s'", Paths::appPath().toUtf8().data());
|
|---|
| 397 | qDebug(" * data path: '%s'", Paths::dataPath().toUtf8().data());
|
|---|
| 398 | qDebug(" * translation path: '%s'", Paths::translationPath().toUtf8().data());
|
|---|
| 399 | qDebug(" * doc path: '%s'", Paths::docPath().toUtf8().data());
|
|---|
| 400 | qDebug(" * themes path: '%s'", Paths::themesPath().toUtf8().data());
|
|---|
| 401 | qDebug(" * shortcuts path: '%s'", Paths::shortcutsPath().toUtf8().data());
|
|---|
| 402 | qDebug(" * config path: '%s'", Paths::configPath().toUtf8().data());
|
|---|
| 403 | qDebug(" * ini path: '%s'", Paths::iniPath().toUtf8().data());
|
|---|
| 404 | qDebug(" * file for subtitles' styles: '%s'", Paths::subtitleStyleFile().toUtf8().data());
|
|---|
| 405 | qDebug(" * current path: '%s'", QDir::currentPath().toUtf8().data());
|
|---|
| 406 | }
|
|---|