File: main.cpp

package info (click to toggle)
classified-ads 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,772 kB
  • sloc: cpp: 34,291; tcl: 1,175; xml: 64; makefile: 40
file content (216 lines) | stat: -rw-r--r-- 7,896 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*     -*-C++-*- -*-coding: utf-8-unix;-*-
  Classified Ads is Copyright (c) Antti Järvinen 2013-2017.

  This file is part of Classified Ads.

  Classified Ads is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  Classified Ads is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with Classified Ads; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef WIN32
#include <winsock2.h>
#endif
#include "controller.h"
#include <signal.h>
#include "log.h"
#include <QHostAddress> // for Q_IPV6ADDR
#include <QApplication>
#include "util/catranslator.h"
#include <QRegExpValidator>

QApplication* app ; /**< The qt application, we need to have 1 instance */
MController* controllerInstanceEx ; /**< Application controller,
                                     used in test suite+tcl wrapper */
Controller* controllerInstance ; /**< Application controller,
                                    here as static for signal handlers */
/** ipv6 addr with all bits zero, to denote an invalid addr */
Q_IPV6ADDR KNullIpv6Addr ( QHostAddress("::0").toIPv6Address () ) ;
/**
 * Hash with all bits zero, to denote an invalid or non-used hash.
 * Some lucky dudette will have this generated for her profile fingerprint  :)
 */
Hash KNullHash ;
#ifndef WIN32
/**
 * SIGINT handler is trapped into this function ;
 * this will try to semi-gracefully terminate the
 * application
 */
void sigINThandler(int) {
    LOG_STR("SIGINT trapped..") ;
    if ( app != NULL ) {
        QApplication::quit() ;
    }
}

/**
 * SIGUSR1 handler is trapped into this function ;
 * lets have that for hiding the UI
 */
void sigUSR1handler(int) {
    LOG_STR("SIGUSR1 trapped..") ;
    if ( controllerInstance != NULL ) {
        controllerInstance->hideUI() ;
    }
}

/**
 * SIGUSR2 handler is trapped into this function ;
 * lets have that for showing the hidden UI
 */
void sigUSR2handler(int) {
    LOG_STR("SIGUSR2 trapped..") ;
    if ( controllerInstance != NULL ) {
        controllerInstance->showUI() ;
        controllerInstance->checkForSharedMemoryContents() ;
    }
}
#endif

/**
 * FZ - in the night of the iron sausage
 */
int main(int argc, char *argv[]) {
    KNullHash = Hash() ;
#ifdef WIN32
    WSADATA wsaData;
    int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if(nResult != NO_ERROR) {
        QLOG_STR( "WSAStartup() failed.");
    } else {
        QLOG_STR( "WSAStartup() success");
    }
#endif
#if !(defined(WIN32)||defined(Q_OS_OSX))
#if QT_VERSION >= 0x050000
    QString platform ( QGuiApplication::platformName() ) ;
    bool have_xcb ( platform.compare("xcb") == 0 || platform.length()==0) ;
    QLOG_STR("Platform: " + QGuiApplication::platformName() +
             " len = " +
             QString::number(platform.length())) ;
    bool have_display ( getenv("DISPLAY") != NULL  ) ;
    bool have_wayland ( getenv("WAYLAND_DISPLAY") != NULL ) ;
    if ( have_xcb && (
                have_display==false &&
                have_wayland==false ) ) {
        // so, we have "xcb" that is normal linux. and we have no $DISPLAY
        // nor $WAYLAND_DISPLAY -> this spells some problems..
        fprintf(stderr,"No $DISPLAY/WAYLAND_DISPLAY environment variable set, cant continue\n") ;
        return 0 ;
    }
#endif // qt version
#endif // check of $DISPLAY or $WAYLAND_DISPLAY

    app = new QApplication (argc, argv);
#ifdef DEBUG
    QLoggingCategory::setFilterRules("*.debug=true");
#endif
#ifdef WIN32
    // In windows it is necessary to tell TCL where
    // its runtime resides:
    if ( getenv("TCL_LIBRARY") == NULL  ) {
      // TCL library variable is not set, set:
      QString tclLib ( "TCL_LIBRARY=" +
		       QCoreApplication::applicationDirPath() +
		       "/tcl8.6") ; 
      QLOG_STR("Setting TCL library to " + tclLib) ; 
      putenv(tclLib.toUtf8().constData()) ; 
    }
    QString tclLibValue ; 
    if ( getenv("TCLLIBPATH") != NULL  ) {
      char *tclLibEnvValue ( getenv("TCLLIBPATH") ) ; 
      tclLibValue.append(QString ( tclLibEnvValue ) ) ; 
    } 
    if ( tclLibValue.toLower().contains("tk8") == false ) {
      // tk is not mentioned in library path, put it there
      if ( tclLibValue.isEmpty() == false ) {
	tclLibValue.append(" ") ; // separating whitespace
      }
      QString tclLibSetCmd  ( "TCLLIBPATH=" +
			      tclLibValue + 
			      QCoreApplication::applicationDirPath() +
			      "/tk8.6") ; 
      QLOG_STR("Setting TCLLIBPATH to " + tclLibSetCmd) ; 
      putenv(tclLibSetCmd.toUtf8().constData()) ; 
    }
#endif
    CATranslator caTranslator;

    if ( caTranslator.load( "qt_" + QLocale::system().name()) == false )  {
        QLOG_STR("Trying translations from " + QLibraryInfo::location(QLibraryInfo::TranslationsPath)) ;
        if ( caTranslator.load("qt_" + QLocale::system().name(),
                               QLibraryInfo::location(QLibraryInfo::TranslationsPath)) == true ) {
            QLOG_STR("Qt translation loaded from "  +
                     QLibraryInfo::location(QLibraryInfo::TranslationsPath)) ;
        } else {
#ifndef WIN32
            if ( caTranslator.load(QLibraryInfo::location(QLibraryInfo::TranslationsPath) + "/qt_" + QLocale::system().name())  ) {
                QLOG_STR("Qt translation found from "+QLibraryInfo::location(QLibraryInfo::TranslationsPath) + " using direct file naming") ;
            } else {
                QLOG_STR("Qt translation not found") ;
            }
#else
            QLOG_STR("Qt translation not found") ;
#endif
        }
    } else {
        QLOG_STR("Qt translation found from current directory") ;
    }
    app->installTranslator(&caTranslator);

    // controller will actually start launching the application
    controllerInstance = new Controller(*app) ;
    controllerInstanceEx = controllerInstance ;  
#if QT_VERSION < 0x050000
    // without this qt4+qjson does not handle utf-8 well ; every
    // byte in multi-byte unicode-sequences appears as separate
    // character after the string is parsed back in windows environment.
    // linux does some magick tricks to not fail due to broken utf-8.
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);
#endif
#ifndef WIN32
    signal(SIGINT,sigINThandler); // if user presses CTRL-C
    signal(SIGHUP,sigINThandler); // if user closed the terminal..
    signal(SIGUSR1,sigUSR1handler);
    signal(SIGUSR2,sigUSR2handler);
#endif

    // check for possible command line arguments relevant to
    // us:
    QRegExp rx("^(caprofile|caad|cacomment|cablob)://[a-fA-F0-9]{40}/{0,1}$");
    QRegExpValidator validator (rx);
    for ( int i = 1 ; i < argc ; i++ ) {
        QString argumentCandidate(argv[i] );
        int position ( 0 ) ;
        if ( validator.validate(argumentCandidate,position) == QValidator::Acceptable ) {
            QUrl commandLineUrl ( argumentCandidate ) ;
            QLOG_STR("scheme " + commandLineUrl.scheme() ) ;
            QLOG_STR("host " + commandLineUrl.host() ) ;
            controllerInstance->addObjectToOpen(commandLineUrl) ;
            break ; // out of the loop, process only one
        }
    }
    int retval ( 0 ) ;
    if ( controllerInstance->init() ) { // 2nd stage of constructor
        retval = app->exec() ;
    }
    QLOG_STR("deleting controller") ;
    delete controllerInstance ;
    delete app ;
    return retval ;
}