File: main.cc

package info (click to toggle)
goldendict 0.9.1~git20100719-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 8,992 kB
  • ctags: 3,672
  • sloc: cpp: 22,301; ansic: 10,244; makefile: 53; sh: 9
file content (186 lines) | stat: -rw-r--r-- 4,617 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* This file is (c) 2008-2010 Konstantin Isakov <ikm@users.berlios.de>
 * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */

#include <stdio.h>
#include <QIcon>
#include "mainwindow.hh"
#include "config.hh"

#include "processwrapper.hh"
#include "hotkeywrapper.hh"

//#define __DO_DEBUG

#ifdef __DO_DEBUG
#include <sys/resource.h>
#endif

#include "termination.hh"

#include <QWebSecurityOrigin>

int main( int argc, char ** argv )
{
  // The following clause fixes a race in the MinGW runtime where throwing
  // exceptions for the first time in several threads simultaneously can cause
  // an abort(). This code throws first exception in a safe, single-threaded
  // manner, thus avoiding that race.
  {
    class Dummy {};

    try
    { throw Dummy(); }
    catch( Dummy )
    {}
  }

  if ( argc == 3 && strcmp( argv[ 1 ], "--show-error-file" ) == 0 )
  {
    // The program has crashed -- show a message about it

    QApplication app( argc, argv );

    QFile errFile( argv[ 2 ] );

    QString errorText;

    if ( errFile.open( QFile::ReadOnly ) )
      errorText = QString::fromUtf8( errFile.readAll() );

    errorText += "\n" + QString( "This information is located in file %1, "
                                 "which will be removed once you close this dialog.").arg( errFile.fileName() );

    QMessageBox::critical( 0, "GoldenDict has crashed", errorText );

    errFile.remove();

    return 0;
  }

  installTerminationHandler();

  #ifdef __DO_DEBUG
  {
    rlimit limit;

    memset( &limit, 0, sizeof( limit ) );
    limit.rlim_cur = RLIM_INFINITY;
    limit.rlim_max = RLIM_INFINITY;
    setrlimit( RLIMIT_CORE, &limit );
  }
  #endif

#ifdef __WIN32

  // Under Windows, increase the amount of fopen()-able file descriptors from
  // the default 512 up to 2048.
  _setmaxstdio( 2048 );

#endif

  QHotkeyApplication app( argc, argv );

  app.setApplicationName( "GoldenDict" );
  app.setOrganizationDomain( "http://goldendict.berlios.de/" );

  app.setWindowIcon( QIcon( ":/icons/programicon.png" ) );

  Config::Class cfg( Config::load() );

  if ( Config::isPortableVersion() )
  {
    // For portable version, hardcode some settings

    cfg.paths.clear();
    cfg.paths.push_back( Config::Path( Config::getPortableVersionDictionaryDir(), true ) );
    cfg.soundDirs.clear();
    cfg.hunspell.dictionariesPath = Config::getPortableVersionMorphoDir();
  }

  // Prevent execution of the 2nd copy

  // get pid
  quint64 current_pid = QCoreApplication::applicationPid();

//  QString app_fname = QFileInfo(QCoreApplication::applicationFilePath()).baseName();
//  quint64 pid = ProcessWrapper::findProcess(
//          app_fname.toAscii().data(),
//          current_pid);
//
//  qDebug() << "pid " << pid;
//  qDebug() << "current_pid " << current_pid;

  // check pid file
  QFile pid_file( Config::getPidFileName() );

  if ( pid_file.exists() ) // pid file exists, check it
  {
    pid_file.open( QIODevice::ReadWrite );
    QDataStream ds( &pid_file );
    quint64 tmp; ds >> tmp;
    pid_file.close();

    bool isExist = ProcessWrapper::processExists(tmp);
    if ( isExist && tmp != current_pid )
    {
      puts( "Another GoldenDict copy started already." );
      return 1;
    }

//    if ( tmp == pid ) // it is active - exiting
//    {
//      // to do: switch to pid ?
//      return 1;
//    }
  }

  pid_file.open( QIODevice::WriteOnly );
  QDataStream ds( &pid_file );
  ds << current_pid;
  pid_file.close();


  // Load translations

  QTranslator qtTranslator;

  QString localeName = cfg.preferences.interfaceLanguage;

  if ( localeName.isEmpty() )
    localeName = QLocale::system().name();

  if ( !qtTranslator.load( "qt_" + localeName,
                           QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
    qtTranslator.load( "qt_" + localeName, Config::getProgramDataDir() + "/locale/" );

  app.installTranslator( &qtTranslator );

  QTranslator translator;

  translator.load( Config::getLocDir() + "/" + localeName );

  app.installTranslator( &translator );

  // Prevent app from quitting spontaneously when it works with scan popup
  // and with the main window closed.
  app.setQuitOnLastWindowClosed( false );

#if QT_VERSION >= 0x040600
  // Add the dictionary scheme we use as local, so that the file:// links would
  // work in the articles. The function was introduced in Qt 4.6.
  QWebSecurityOrigin::addLocalScheme( "gdlookup" );
#endif

  MainWindow m( cfg );

  app.addDataCommiter( m );

  int r = app.exec();

  app.removeDataCommiter( m );

  // remove pid file
  pid_file.remove();

  return r;
}