~ubuntu-branches/ubuntu/wily/smplayer/wily

« back to all changes in this revision

Viewing changes to src/old-code/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Maia Kozheva
  • Date: 2009-03-31 23:05:43 UTC
  • mto: (1.1.9 upstream) (3.1.2 squeeze)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20090331230543-nsklbxenl2hf2n6h
ImportĀ upstreamĀ versionĀ 0.6.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  smplayer, GUI front-end for mplayer.
2
 
    Copyright (C) 2006-2008 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
 
 
20
 
#include "defaultgui.h"
21
 
#include "minigui.h"
22
 
#include "helper.h"
23
 
#include "global.h"
24
 
#include "preferences.h"
25
 
#include "translator.h"
26
 
#include "version.h"
27
 
#include "config.h"
28
 
#include "myclient.h"
29
 
#include "constants.h"
30
 
#include "clhelp.h"
31
 
 
32
 
#ifdef Q_OS_WIN
33
 
#include "extensions.h"
34
 
#include "winfileassoc.h"       //required for Uninstall
35
 
#endif
36
 
 
37
 
#include <QApplication>
38
 
#include <QLocale>
39
 
#include <QTranslator>
40
 
#include <QFileInfo>
41
 
#include <QDir>
42
 
#include <QSettings>
43
 
#include <QRegExp>
44
 
 
45
 
#include <stdio.h>
46
 
#include <stdlib.h>
47
 
 
48
 
using namespace Global;
49
 
 
50
 
static QRegExp rx_log;
51
 
 
52
 
void myMessageOutput( QtMsgType type, const char *msg ) {
53
 
        if ( (!pref) || (!pref->log_smplayer) ) return;
54
 
 
55
 
        rx_log.setPattern(pref->log_filter);
56
 
 
57
 
        QString line = QString::fromUtf8(msg);
58
 
        switch ( type ) {
59
 
                case QtDebugMsg:
60
 
                        if (rx_log.indexIn(line) > -1) {
61
 
                                #ifndef NO_DEBUG_ON_CONSOLE
62
 
                                fprintf( stderr, "Debug: %s\n", line.toLocal8Bit().data() );
63
 
                                #endif
64
 
                                Helper::addLog( line );
65
 
                        }
66
 
                        break;
67
 
                case QtWarningMsg:
68
 
                        #ifndef NO_DEBUG_ON_CONSOLE
69
 
                        fprintf( stderr, "Warning: %s\n", line.toLocal8Bit().data() );
70
 
                        #endif
71
 
                        Helper::addLog( "WARNING: " + line );
72
 
                        break;
73
 
                case QtFatalMsg:
74
 
                        #ifndef NO_DEBUG_ON_CONSOLE
75
 
                        fprintf( stderr, "Fatal: %s\n", line.toLocal8Bit().data() );
76
 
                        #endif
77
 
                        Helper::addLog( "FATAL: " + line );
78
 
                        abort();                    // deliberately core dump
79
 
                case QtCriticalMsg:
80
 
                        #ifndef NO_DEBUG_ON_CONSOLE
81
 
                        fprintf( stderr, "Critical: %s\n", line.toLocal8Bit().data() );
82
 
                        #endif
83
 
                        Helper::addLog( "CRITICAL: " + line );
84
 
                        break;
85
 
        }
86
 
}
87
 
 
88
 
void showInfo() {
89
 
        QString s = QObject::tr("This is SMPlayer v. %1 running on %2")
90
 
            .arg(smplayerVersion())
91
 
#ifdef Q_OS_LINUX
92
 
           .arg("Linux")
93
 
#else
94
 
#ifdef Q_OS_WIN
95
 
           .arg("Windows")
96
 
#else
97
 
                   .arg("Other OS")
98
 
#endif
99
 
#endif
100
 
           ;
101
 
 
102
 
        printf("%s\n", s.toLocal8Bit().data() );
103
 
        qDebug("%s", s.toUtf8().data() );
104
 
        qDebug("Qt v. " QT_VERSION_STR);
105
 
 
106
 
        qDebug(" * application path: '%s'", Helper::appPath().toUtf8().data());
107
 
        qDebug(" * data path: '%s'", Helper::dataPath().toUtf8().data());
108
 
        qDebug(" * translation path: '%s'", Helper::translationPath().toUtf8().data());
109
 
        qDebug(" * doc path: '%s'", Helper::docPath().toUtf8().data());
110
 
        qDebug(" * themes path: '%s'", Helper::themesPath().toUtf8().data());
111
 
        qDebug(" * shortcuts path: '%s'", Helper::shortcutsPath().toUtf8().data());
112
 
        qDebug(" * smplayer home path: '%s'", Helper::appHomePath().toUtf8().data());
113
 
        qDebug(" * ini path: '%s'", Helper::iniPath().toUtf8().data());
114
 
        qDebug(" * current path: '%s'", QDir::currentPath().toUtf8().data());
115
 
}
116
 
 
117
 
 
118
 
void createHomeDirectory() {
119
 
        // Create smplayer home directories
120
 
        if (!QFile::exists(Helper::appHomePath())) {
121
 
                QDir d;
122
 
                if (!d.mkdir(Helper::appHomePath())) {
123
 
                        qWarning("main: can't create %s", Helper::appHomePath().toUtf8().data());
124
 
                }
125
 
                QString s = Helper::appHomePath() + "/screenshots";
126
 
                if (!d.mkdir(s)) {
127
 
                        qWarning("main: can't create %s", s.toUtf8().data());
128
 
                }
129
 
        }
130
 
}
131
 
 
132
 
int main( int argc, char ** argv ) 
133
 
{
134
 
        QApplication a( argc, argv );
135
 
 
136
 
#ifdef Q_OS_WIN
137
 
        if (a.arguments().contains("-uninstall")){
138
 
                //Called by uninstaller. Will restore old associations.
139
 
                WinFileAssoc RegAssoc; 
140
 
                Extensions exts; 
141
 
                QStringList regExts; 
142
 
                RegAssoc.GetRegisteredExtensions(exts.multimedia(), regExts); 
143
 
                RegAssoc.RestoreFileAssociations(regExts); 
144
 
                printf("Restored associations\n");
145
 
                return 0; 
146
 
        }
147
 
#endif
148
 
 
149
 
 
150
 
 
151
 
        QString app_path = a.applicationDirPath();
152
 
        Helper::setAppPath(app_path);
153
 
        //qDebug( "main: application path: '%s'", app_path.toUtf8().data());
154
 
 
155
 
        QString ini_path="";
156
 
        QStringList files_to_play;
157
 
        QString action; // Action to be passed to running instance
158
 
        QString actions_list; // Actions to be run on startup
159
 
        bool add_to_playlist = false;
160
 
 
161
 
        QString app_name = QFileInfo(a.applicationFilePath()).baseName();
162
 
        qDebug("main: app name: %s", app_name.toUtf8().data());
163
 
        // If the name is smplayer_portable, activate the -ini_path by default
164
 
        if (app_name.toLower() == "smplayer_portable")  {
165
 
                ini_path = Helper::appPath();
166
 
        } 
167
 
        else if (QFile::exists( Helper::appPath() + "/smplayer.ini" ) ) {
168
 
                ini_path = Helper::appPath();
169
 
                qDebug("Using existing %s", QString(Helper::appPath() + "/smplayer.ini").toUtf8().data());
170
 
        }
171
 
 
172
 
        int close_at_end = -1; // -1 = not set, 1 = true, 0 false
173
 
        int start_in_fullscreen = -1;
174
 
        bool show_help = false;
175
 
 
176
 
        bool use_minigui = false;
177
 
 
178
 
        // Deleted KDE code
179
 
        // ...
180
 
 
181
 
        // Qt code
182
 
        int arg_init = 1;
183
 
        int arg_count = a.arguments().count();
184
 
 
185
 
        bool is_playlist = false;
186
 
 
187
 
        if ( arg_count > arg_init ) {
188
 
                for (int n=arg_init; n < arg_count; n++) {
189
 
                        QString argument = a.arguments()[n];
190
 
                        if (argument == "-ini-path") {
191
 
                                //qDebug( "ini_path: %d %d", n+1, arg_count );
192
 
                                ini_path = Helper::appPath();
193
 
                                if (n+1 < arg_count) {
194
 
                                        n++;
195
 
                                        ini_path = a.arguments()[n];
196
 
                                } else {
197
 
                                        printf("Error: expected parameter for -ini-path\r\n");
198
 
                                        return -1;
199
 
                                }
200
 
                        }
201
 
                        else
202
 
                        if (argument == "-send-action") {
203
 
                                if (n+1 < arg_count) {
204
 
                                        n++;
205
 
                                        action = a.arguments()[n];
206
 
                                } else {
207
 
                                        printf("Error: expected parameter for -send-action\r\n");
208
 
                                        return -1;
209
 
                                }
210
 
                        }
211
 
                        else
212
 
                        if (argument == "-actions") {
213
 
                                if (n+1 < arg_count) {
214
 
                                        n++;
215
 
                                        actions_list = a.arguments()[n];
216
 
                                } else {
217
 
                                        printf("Error: expected parameter for -actions\r\n");
218
 
                                        return -1;
219
 
                                }
220
 
                        }
221
 
                        else
222
 
                        if (argument == "-playlist") {
223
 
                                is_playlist = true;
224
 
                        }
225
 
                        else
226
 
                        if ((argument == "--help") || (argument == "-help") ||
227
 
                (argument == "-h") || (argument == "-?") ) {
228
 
                                show_help = true;
229
 
                        }
230
 
                        else
231
 
                        if (argument == "-close-at-end") {
232
 
                                close_at_end = 1;
233
 
                        }
234
 
                        else
235
 
                        if (argument == "-no-close-at-end") {
236
 
                                close_at_end = 0;
237
 
                        }
238
 
                        else
239
 
                        if (argument == "-fullscreen") {
240
 
                                start_in_fullscreen = 1;
241
 
                        }
242
 
                        else
243
 
                        if (argument == "-no-fullscreen") {
244
 
                                start_in_fullscreen = 0;
245
 
                        }
246
 
                        else
247
 
                        if (argument == "-add-to-playlist") {
248
 
                                add_to_playlist = true;
249
 
                        }
250
 
                        else
251
 
                        if (argument == "-mini") {
252
 
                                use_minigui = true;
253
 
                        }
254
 
                        else {
255
 
                                // File
256
 
                                if (QFile::exists( argument )) {
257
 
                                        argument = QFileInfo(argument).absoluteFilePath();
258
 
                                }
259
 
                                if (is_playlist) {
260
 
                                        argument = argument + IS_PLAYLIST_TAG;
261
 
                                        is_playlist = false;
262
 
                                }
263
 
                                files_to_play.append( argument );
264
 
                        }
265
 
                }
266
 
        }
267
 
 
268
 
        if (ini_path.isEmpty()) createHomeDirectory();
269
 
 
270
 
        global_init(ini_path);
271
 
 
272
 
        qInstallMsgHandler( myMessageOutput );
273
 
 
274
 
        // Application translations
275
 
        translator->load( pref->language );
276
 
 
277
 
        showInfo();
278
 
 
279
 
        if (show_help) {
280
 
                printf("%s\n", CLHelp::help().toLocal8Bit().data());
281
 
                return 0;
282
 
        }
283
 
 
284
 
        qDebug("main: files_to_play: count: %d", files_to_play.count() );
285
 
        for (int n=0; n < files_to_play.count(); n++) {
286
 
                qDebug("main: files_to_play[%d]: '%s'", n, files_to_play[n].toUtf8().data());
287
 
        }
288
 
 
289
 
 
290
 
        if (pref->use_single_instance) {
291
 
                // Single instance
292
 
                MyClient *c = new MyClient(pref->connection_port);
293
 
                //c->setTimeOut(1000);
294
 
                if (c->openConnection()) {
295
 
                        qDebug("main: found another instance");
296
 
 
297
 
                        if (!action.isEmpty()) {
298
 
                                if (c->sendAction(action)) {
299
 
                                        qDebug("main: action passed successfully to the running instance");
300
 
                                } else {
301
 
                                        printf("Error: action couldn't be passed to the running instance");
302
 
                                        return -1;
303
 
                                }
304
 
                        }
305
 
                        else    
306
 
                        if (!files_to_play.isEmpty()) {
307
 
                                if (c->sendFiles(files_to_play, add_to_playlist)) {
308
 
                                        qDebug("main: files sent successfully to the running instance");
309
 
                        qDebug("main: exiting.");
310
 
                                } else {
311
 
                                        qDebug("main: files couldn't be sent to another instance");
312
 
                                }
313
 
                        }
314
 
 
315
 
                        return 0;
316
 
 
317
 
                } else {
318
 
                        if (!action.isEmpty()) {
319
 
                                printf("Error: no running instance found\r\n");
320
 
                                return -1;
321
 
                        }
322
 
                }
323
 
        }
324
 
 
325
 
        if (!pref->default_font.isEmpty()) {
326
 
                QFont f;
327
 
                f.fromString(pref->default_font);
328
 
                a.setFont(f);
329
 
        }
330
 
 
331
 
        if (close_at_end != -1) {
332
 
                pref->close_on_finish = close_at_end;
333
 
        }
334
 
 
335
 
        if (start_in_fullscreen != -1) {
336
 
                pref->start_in_fullscreen = start_in_fullscreen;
337
 
        }
338
 
 
339
 
        // Changes to app path, so smplayer can find a relative mplayer path
340
 
        QDir::setCurrent(Helper::appPath());
341
 
        qDebug("main: changed working directory to app path");
342
 
        qDebug("main: current directory: %s", QDir::currentPath().toUtf8().data());
343
 
 
344
 
        BaseGui * w;
345
 
        if (use_minigui)
346
 
                w = new MiniGui(0);
347
 
        else
348
 
                w = new DefaultGui(0);
349
 
 
350
 
        if (!w->startHidden() || !files_to_play.isEmpty() ) w->show();
351
 
        if (!files_to_play.isEmpty()) w->openFiles(files_to_play);
352
 
 
353
 
        if (!actions_list.isEmpty()) w->runActions(actions_list);
354
 
 
355
 
        a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
356
 
 
357
 
        int r = a.exec();
358
 
        delete w;
359
 
 
360
 
        global_end();
361
 
 
362
 
        return r;
363
 
}