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/main.cpp

    r124 r128  
    1717*/
    1818
    19 #include <QApplication>
    20 #include <QFile>
    21 #include <QTime>
    22 #include <QDir>
    23 
     19#include "myapplication.h"
    2420#include "smplayer.h"
    25 #include "global.h"
    26 #include "helper.h"
    27 #include "paths.h"
    28 
    29 #include <stdio.h>
    30 
    31 #if USE_QTLOCKEDFILE
    32 #define USE_LOCKS 1
    33 #if USE_LOCKS
    34 #include "qtlockedfile/qtlockedfile.h"
    35 #endif
    36 #endif
    37 
    38 using namespace Global;
    39 
    40 BaseGui * basegui_instance = 0;
    41 
    42 QFile output_log;
    43 
    44 void myMessageOutput( QtMsgType type, const char *msg ) {
    45         static QStringList saved_lines;
    46         static QString orig_line;
    47         static QString line2;
    48         static QRegExp rx_log;
    49 
    50         if (pref) {
    51                 if (!pref->log_smplayer) return;
    52                 rx_log.setPattern(pref->log_filter);
    53         } else {
    54                 rx_log.setPattern(".*");
    55         }
    56 
    57         line2.clear();
    58 
    59         orig_line = QString::fromUtf8(msg);
    60 
    61         switch ( type ) {
    62                 case QtDebugMsg:
    63                         if (rx_log.indexIn(orig_line) > -1) {
    64                                 #ifndef NO_DEBUG_ON_CONSOLE
    65                                 fprintf( stderr, "Debug: %s\n", orig_line.toLocal8Bit().data() );
    66                                 #endif
    67                                 line2 = orig_line;
    68                         }
    69                         break;
    70                 case QtWarningMsg:
    71                         #ifndef NO_DEBUG_ON_CONSOLE
    72                         fprintf( stderr, "Warning: %s\n", orig_line.toLocal8Bit().data() );
    73                         #endif
    74                         line2 = "WARNING: " + orig_line;
    75                         break;
    76                 case QtFatalMsg:
    77                         #ifndef NO_DEBUG_ON_CONSOLE
    78                         fprintf( stderr, "Fatal: %s\n", orig_line.toLocal8Bit().data() );
    79                         #endif
    80                         line2 = "FATAL: " + orig_line;
    81                         abort();                    // deliberately core dump
    82                 case QtCriticalMsg:
    83                         #ifndef NO_DEBUG_ON_CONSOLE
    84                         fprintf( stderr, "Critical: %s\n", orig_line.toLocal8Bit().data() );
    85                         #endif
    86                         line2 = "CRITICAL: " + orig_line;
    87                         break;
    88         }
    89 
    90         if (line2.isEmpty()) return;
    91 
    92         line2 = "["+ QTime::currentTime().toString("hh:mm:ss:zzz") +"] "+ line2;
    93 
    94         if (basegui_instance) {
    95                 if (!saved_lines.isEmpty()) {
    96                         // Send saved lines first
    97                         for (int n=0; n < saved_lines.count(); n++) {
    98                                 basegui_instance->recordSmplayerLog(saved_lines[n]);
    99                         }
    100                         saved_lines.clear();
    101                 }
    102                 basegui_instance->recordSmplayerLog(line2);
    103         } else {
    104                 // GUI is not created yet, save lines for later
    105                 saved_lines.append(line2);
    106         }
    107 
    108         if (pref) {
    109                 if (pref->save_smplayer_log) {
    110                         // Save log to file
    111                         if (!output_log.isOpen()) {
    112                                 // FIXME: the config path may not be initialized if USE_LOCKS is not defined
    113                                 output_log.setFileName( Paths::configPath() + "/smplayer_log.txt" );
    114                                 output_log.open(QIODevice::WriteOnly);
    115                         }
    116                         if (output_log.isOpen()) {
    117                                 QString l = line2 + "\r\n";
    118                                 output_log.write(l.toUtf8().constData());
    119                                 output_log.flush();
    120                         }
    121                 }
    122         }
    123 }
    124 
    125 
    126 class MyApplication : public QApplication
    127 {
    128 public:
    129         MyApplication ( int & argc, char ** argv ) : QApplication(argc, argv) {};
    130         virtual void commitData ( QSessionManager & /*manager*/ ) {
    131                 // Nothing to do, let the application to close
    132         }
    133 };
    13421
    13522int main( int argc, char ** argv )
    13623{
    137         MyApplication a( argc, argv );
     24        MyApplication a( "smplayer", argc, argv );
     25        /*
     26        if (a.isRunning()) {
     27                qDebug("Another instance is running. Exiting.");
     28                return 0;
     29        }
     30        */
     31
    13832        a.setQuitOnLastWindowClosed(false);
    139         //a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
    14033
    14134#if QT_VERSION >= 0x040400
     
    17366        }
    17467
    175     qInstallMsgHandler( myMessageOutput );
    176 
    177 #if USE_LOCKS
    178         //setIniPath will be set later in global_init, but we need it here
    179         if (!config_path.isEmpty()) Paths::setConfigPath(config_path);
    180 
    181         QString lock_file = Paths::iniPath() + "/smplayer_init.lock";
    182         qDebug("main: lock_file: %s", lock_file.toUtf8().data());
    183 
    184         QtLockedFile lk(lock_file);
    185         lk.open(QFile::ReadWrite);
    186 
    187         if (QDir().exists(Paths::iniPath())) {
    188                 bool lock_ok = lk.lock(QtLockedFile::WriteLock, false);
    189 
    190                 if (!lock_ok) {
    191                         //lock failed
    192                         qDebug("main: lock failed");
    193 
    194                         // Wait 10 secs max.
    195                         int n = 100;
    196                         while ( n > 0) {
    197                                 Helper::msleep(100); // wait 100 ms
    198 
    199                                 if (lk.lock(QtLockedFile::WriteLock, false)) break;
    200                                 n--;
    201                                 if ((n % 10) == 0) qDebug("main: waiting %d...", n);
    202                         }
    203                         // Continue startup
    204                 }
    205         }
    206 #endif // USE_LOCKS
    207 
    20868        SMPlayer * smplayer = new SMPlayer(config_path);
    20969        SMPlayer::ExitCode c = smplayer->processArgs( args );
    21070        if (c != SMPlayer::NoExit) {
    211 #if USE_LOCKS
    212                 lk.unlock();
    213 #endif
    21471                return c;
    21572        }
    216 
    217         basegui_instance = smplayer->gui();
    218         a.connect(smplayer->gui(), SIGNAL(quitSolicited()), &a, SLOT(quit()));
    21973        smplayer->start();
    220 
    221 #if USE_LOCKS
    222         bool success = lk.unlock();
    223         qDebug("Unlocking: %d", success);
    224 #endif
    22574
    22675        int r = a.exec();
    22776
    228         basegui_instance = 0;
    22977        delete smplayer;
    230 
    231         if (output_log.isOpen()) output_log.close();
    23278
    23379        return r;
Note: See TracChangeset for help on using the changeset viewer.