Ignore:
Timestamp:
Mar 29, 2012, 4:53:15 PM (13 years ago)
Author:
Silvan Scherrer
Message:

SMPlayer: trunk update to latest svn

Location:
smplayer/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • smplayer/trunk

  • smplayer/trunk/src/smplayer.cpp

    r124 r128  
    2626#include "version.h"
    2727#include "config.h"
    28 #include "myclient.h"
    2928#include "clhelp.h"
     29#include "myapplication.h"
    3030
    3131#include <QDir>
    32 #include <QApplication>
    3332#include <QUrl>
    34 
     33#include <QTime>
    3534#include <stdio.h>
    3635
     
    4241#endif
    4342
    44 
    4543using namespace Global;
     44
     45BaseGui * SMPlayer::main_window = 0;
    4646
    4747SMPlayer::SMPlayer(const QString & config_path, QObject * parent )
    4848        : QObject(parent)
    4949{
    50         main_window = 0;
     50#ifdef LOG_SMPLAYER
     51        qInstallMsgHandler( SMPlayer::myMessageOutput );
     52        allow_to_send_log_to_gui = true;
     53#endif
     54
    5155        gui_to_use = "DefaultGui";
    5256
    5357        close_at_end = -1; // Not set
    5458        start_in_fullscreen = -1; // Not set
    55         use_control_server = true;
    5659
    5760        move_gui = false;
    5861        resize_gui = false;
    5962
    60     Paths::setAppPath( qApp->applicationDirPath() );
     63        Paths::setAppPath( qApp->applicationDirPath() );
    6164
    6265#ifndef PORTABLE_APP
     
    7174
    7275SMPlayer::~SMPlayer() {
    73         if (main_window != 0) delete main_window;
     76        if (main_window != 0) {
     77                deleteGUI();
     78        }
    7479        global_end();
     80
     81#ifdef LOG_SMPLAYER
     82        if (output_log.isOpen()) output_log.close();
     83#endif
    7584}
    7685
     
    8190                qDebug("SMPlayer::gui: changed working directory to app path");
    8291                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);
     92
     93                main_window = createGUI(gui_to_use);
    9194
    9295                if (move_gui) {
     
    98101                        main_window->resize(gui_size);
    99102                }
    100 
    101                 main_window->setForceCloseOnFinish(close_at_end);
    102                 main_window->setForceStartInFullscreen(start_in_fullscreen);
    103103        }
    104104
    105105        return main_window;
    106106}
     107
     108BaseGui * SMPlayer::createGUI(QString gui_name) {
     109        BaseGui * gui = 0;
     110
     111        if (gui_name.toLower() == "minigui")
     112                gui = new MiniGui(0);
     113        else
     114        if (gui_name.toLower() == "mpcgui")
     115                gui = new MpcGui(0);
     116        else
     117                gui = new DefaultGui(0);
     118
     119        gui->setForceCloseOnFinish(close_at_end);
     120        gui->setForceStartInFullscreen(start_in_fullscreen);
     121        connect(gui, SIGNAL(quitSolicited()), qApp, SLOT(quit()));
     122
     123#ifdef GUI_CHANGE_ON_RUNTIME
     124        connect(gui, SIGNAL(guiChanged(QString)), this, SLOT(changeGUI(QString)));
     125#endif
     126
     127#if SINGLE_INSTANCE
     128        MyApplication * app = MyApplication::instance();
     129        connect(app, SIGNAL(messageReceived(const QString&)),
     130            gui, SLOT(handleMessageFromOtherInstances(const QString&)));
     131        app->setActivationWindow(gui);
     132#endif
     133
     134        return gui;
     135}
     136
     137void SMPlayer::deleteGUI() {
     138#ifdef LOG_SMPLAYER
     139        allow_to_send_log_to_gui = false;
     140#endif
     141
     142        delete main_window;
     143        main_window = 0;
     144
     145#ifdef LOG_SMPLAYER
     146        allow_to_send_log_to_gui = true;
     147#endif
     148}
     149
     150#ifdef GUI_CHANGE_ON_RUNTIME
     151void SMPlayer::changeGUI(QString new_gui) {
     152        qDebug("SMPlayer::changeGUI: '%s'", new_gui.toLatin1().constData());
     153
     154        deleteGUI();
     155
     156        main_window = createGUI(new_gui);
     157
     158        main_window->show();
     159}
     160#endif
    107161
    108162SMPlayer::ExitCode SMPlayer::processArgs(QStringList args) {
     
    222276                }
    223277                else
    224                 if (argument == "-disable-server") {
    225                         use_control_server = false;
    226                 }
    227                 else
    228278                if (argument == "-add-to-playlist") {
    229279                        add_to_playlist = true;
     
    266316        }
    267317
    268 
     318#ifdef SINGLE_INSTANCE
    269319        if (pref->use_single_instance) {
    270320                // 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");
     321                MyApplication * a = MyApplication::instance();
     322                if (a->isRunning()) {
     323                        a->sendMessage("Hello");
    280324
    281325                        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                                 }
     326                                a->sendMessage("action " + action);
    288327                        }
    289328                        else {
    290329                                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                                         }
     330                                        a->sendMessage("load_sub " + subtitle_file);
    296331                                }
    297332
    298333                                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                                         }
     334                                        /* a->sendMessage("open_file " + files_to_play[0]); */
     335                                        QString command = "open_files";
     336                                        if (add_to_playlist) command = "add_to_playlist";
     337                                        a->sendMessage(command +" "+ files_to_play.join(" <<sep>> "));
    305338                                }
    306339                        }
    307                         c->closeConnection();
     340
    308341                        return NoError;
    309                 } else {
    310                         if (!action.isEmpty()) {
    311                                 printf("Error: no running instance found\r\n");
    312                                 return NoRunningInstance;
    313                         }
    314                 }
    315         }
     342                }
     343        }
     344#endif
    316345
    317346        if (!pref->default_font.isEmpty()) {
     
    410439        qDebug(" * current path: '%s'", QDir::currentPath().toUtf8().data());
    411440}
     441
     442#ifdef LOG_SMPLAYER
     443QFile SMPlayer::output_log;
     444bool SMPlayer::allow_to_send_log_to_gui = false;
     445
     446void SMPlayer::myMessageOutput( QtMsgType type, const char *msg ) {
     447        static QStringList saved_lines;
     448        static QString orig_line;
     449        static QString line2;
     450        static QRegExp rx_log;
     451
     452        if (pref) {
     453                if (!pref->log_smplayer) return;
     454                rx_log.setPattern(pref->log_filter);
     455        } else {
     456                rx_log.setPattern(".*");
     457        }
     458
     459        line2.clear();
     460
     461        orig_line = QString::fromUtf8(msg);
     462
     463        switch ( type ) {
     464                case QtDebugMsg:
     465                        if (rx_log.indexIn(orig_line) > -1) {
     466                                #ifndef NO_DEBUG_ON_CONSOLE
     467                                fprintf( stderr, "Debug: %s\n", orig_line.toLocal8Bit().data() );
     468                                #endif
     469                                line2 = orig_line;
     470                        }
     471                        break;
     472                case QtWarningMsg:
     473                        #ifndef NO_DEBUG_ON_CONSOLE
     474                        fprintf( stderr, "Warning: %s\n", orig_line.toLocal8Bit().data() );
     475                        #endif
     476                        line2 = "WARNING: " + orig_line;
     477                        break;
     478                case QtFatalMsg:
     479                        #ifndef NO_DEBUG_ON_CONSOLE
     480                        fprintf( stderr, "Fatal: %s\n", orig_line.toLocal8Bit().data() );
     481                        #endif
     482                        line2 = "FATAL: " + orig_line;
     483                        abort();                    // deliberately core dump
     484                case QtCriticalMsg:
     485                        #ifndef NO_DEBUG_ON_CONSOLE
     486                        fprintf( stderr, "Critical: %s\n", orig_line.toLocal8Bit().data() );
     487                        #endif
     488                        line2 = "CRITICAL: " + orig_line;
     489                        break;
     490        }
     491
     492        if (line2.isEmpty()) return;
     493
     494        line2 = "["+ QTime::currentTime().toString("hh:mm:ss:zzz") +"] "+ line2;
     495
     496        if (allow_to_send_log_to_gui && main_window) {
     497                if (!saved_lines.isEmpty()) {
     498                        // Send saved lines first
     499                        for (int n=0; n < saved_lines.count(); n++) {
     500                                main_window->recordSmplayerLog(saved_lines[n]);
     501                        }
     502                        saved_lines.clear();
     503                }
     504                main_window->recordSmplayerLog(line2);
     505        } else {
     506                // GUI is not created yet, save lines for later
     507                saved_lines.append(line2);
     508                /* printf("SMPlayer::myMessageOutput: no gui\n"); */
     509        }
     510
     511        if (pref) {
     512                if (pref->save_smplayer_log) {
     513                        // Save log to file
     514                        if (!output_log.isOpen()) {
     515                                // FIXME: the config path may not be initialized if USE_LOCKS is not defined
     516                                output_log.setFileName( Paths::configPath() + "/smplayer_log.txt" );
     517                                output_log.open(QIODevice::WriteOnly);
     518                        }
     519                        if (output_log.isOpen()) {
     520                                QString l = line2 + "\r\n";
     521                                output_log.write(l.toUtf8().constData());
     522                                output_log.flush();
     523                        }
     524                }
     525        }
     526}
     527#endif
     528
     529/*
     530void myMessageOutput( QtMsgType type, const char *msg ) {
     531        static QString orig_line;
     532        orig_line = QString::fromUtf8(msg);
     533
     534        switch ( type ) {
     535                case QtDebugMsg:
     536                        #ifndef NO_DEBUG_ON_CONSOLE
     537                        fprintf( stderr, "Debug: %s\n", orig_line.toLocal8Bit().data() );
     538                        #endif
     539                        break;
     540
     541                case QtWarningMsg:
     542                        #ifndef NO_DEBUG_ON_CONSOLE
     543                        fprintf( stderr, "Warning: %s\n", orig_line.toLocal8Bit().data() );
     544                        #endif
     545                        break;
     546
     547                case QtCriticalMsg:
     548                        #ifndef NO_DEBUG_ON_CONSOLE
     549                        fprintf( stderr, "Critical: %s\n", orig_line.toLocal8Bit().data() );
     550                        #endif
     551                        break;
     552
     553                case QtFatalMsg:
     554                        #ifndef NO_DEBUG_ON_CONSOLE
     555                        fprintf( stderr, "Fatal: %s\n", orig_line.toLocal8Bit().data() );
     556                        #endif
     557                        abort();                    // deliberately core dump
     558        }
     559}
     560*/
     561
     562#include "moc_smplayer.cpp"
Note: See TracChangeset for help on using the changeset viewer.