source: psi/trunk/src/main.cpp@ 51

Last change on this file since 51 was 2, checked in by dmik, 19 years ago

Imported original Psi 0.10 sources from Affinix

File size: 9.0 KB
Line 
1/*
2 * main.cpp - initialization and profile/settings handling
3 * Copyright (C) 2001-2003 Justin Karneges
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include"main.h"
22
23#include"psiapplication.h"
24#include<qtimer.h>
25
26#include<qprocess.h>
27#include<qimage.h>
28#include<qbitmap.h>
29#include<qtextcodec.h>
30#include<qsettings.h>
31#include<qsocketdevice.h>
32#include<qcombobox.h>
33#include<qcheckbox.h>
34#include<qmessagebox.h>
35#include<qca.h>
36#include<stdlib.h>
37#include<time.h>
38#include"common.h"
39#include"profiles.h"
40#include"profiledlg.h"
41#include"xmpp.h"
42
43// need to remove this later
44#include"hash.h"
45
46#include"eventdlg.h"
47#include"chatdlg.h"
48#ifdef USE_CRASH
49# include"crash.h"
50#endif
51
52#ifdef Q_OS_WIN
53# include <qt_windows.h> // for RegDeleteKey
54#endif
55
56using namespace XMPP;
57
58QTranslator *trans;
59QTranslator *qttrans;
60QString curLang = "en";
61QString curLangName = QT_TR_NOOP("language_name");
62
63void setLang(const QString &lang)
64{
65 //printf("changing lang: [%s]\n", lang.latin1());
66 trans->clear();
67 qttrans->clear();
68 if(lang == "en") {
69 curLang = lang;
70 curLangName = "English";
71 return;
72 }
73
74 QStringList dirs;
75 QString subdir = "";
76 dirs += "." + subdir;
77 dirs += g.pathHome + subdir;
78 dirs += g.pathBase + subdir;
79 for(QStringList::Iterator it = dirs.begin(); it != dirs.end(); ++it) {
80 if(!QFile::exists(*it))
81 continue;
82 if(trans->load("psi_" + lang, *it)) {
83 // try to load qt library translation
84 qttrans->load("qt_" + lang, *it);
85 curLang = lang;
86 return;
87 }
88 }
89}
90
91static void folderkeyRemove(QSettings &settings, const QString &d)
92{
93 QStringList entries;
94 QStringList::Iterator it;
95
96 entries = settings.subkeyList(d);
97 for(it = entries.begin(); it != entries.end(); ++it) {
98 QString str = d + "/" + *it;
99 folderkeyRemove(settings, str);
100 }
101
102 entries = settings.entryList(d);
103 for(it = entries.begin(); it != entries.end(); ++it) {
104 QString str = d + "/" + *it;
105 settings.removeEntry(str);
106 }
107 settings.removeEntry(d);
108}
109
110
111PsiMain::PsiMain(QObject *par)
112:QObject(par)
113{
114 pcon = 0;
115
116 // detect available language packs
117 langs.set("en", "English");
118
119 QStringList dirs;
120 QString subdir = "";
121 dirs += "." + subdir;
122 dirs += g.pathHome + subdir;
123 dirs += g.pathBase + subdir;
124
125 for(QStringList::Iterator it = dirs.begin(); it != dirs.end(); ++it) {
126 if(!QFile::exists(*it))
127 continue;
128 QDir d(*it);
129 QStringList entries = d.entryList();
130 for(QStringList::Iterator it2 = entries.begin(); it2 != entries.end(); ++it2) {
131 if(*it2 == "." || *it2 == "..")
132 continue;
133
134 QString str = *it2;
135 // verify that it is a language file
136 if(str.left(4) != "psi_")
137 continue;
138 int n = str.find('.', 4);
139 if(n == -1)
140 continue;
141 if(str.mid(n) != ".qm")
142 continue;
143 QString lang = str.mid(4, n-4);
144
145 //printf("found [%s], lang=[%s]\n", str.latin1(), lang.latin1());
146
147 // get the language_name
148 QString name = QString("[") + str + "]";
149 QTranslator t(0);
150 if(!t.load(str, *it))
151 continue;
152
153 if(t.contains("@default", "language_name", 0)) {
154 QString s = t.findMessage("@default", "language_name", 0).translation();
155 if(!s.isEmpty())
156 name = s;
157 }
158
159 langs.set(lang, name);
160 }
161 }
162
163 // load simple registry settings
164 QSettings *s = new QSettings;
165 s->setPath("psi.affinix.com", "Psi", QSettings::User); // make Psi load all prefs from HKCU on Windows systems
166 s->insertSearchPath(QSettings::Windows, "/Affinix");
167 s->insertSearchPath(QSettings::Unix, g.pathHome);
168 lastProfile = s->readEntry("/psi/lastProfile");
169 lastLang = s->readEntry("/psi/lastLang");
170 autoOpen = s->readBoolEntry("/psi/autoOpen", FALSE);
171 delete s;
172
173 if(lastLang.isEmpty()) {
174 lastLang = QTextCodec::locale();
175 //printf("guessing locale: [%s]\n", lastLang.latin1());
176 }
177
178 setLang(lastLang);
179
180 if(autoOpen && !lastProfile.isEmpty() && profileExists(lastProfile)) {
181 // Auto-open the last profile
182 activeProfile = lastProfile;
183 QTimer::singleShot(0, this, SLOT(sessionStart()));
184 }
185 else {
186 if (!lastProfile.isEmpty() && !getProfilesList().isEmpty()) {
187 // Select a profile
188 QTimer::singleShot(0, this, SLOT(chooseProfile()));
189 }
190 else {
191 // Create & open the default profile
192 if (!profileExists("default") && !profileNew("default")) {
193 QMessageBox::critical(0, tr("Error"),
194 tr("There was an error creating the default profile."));
195 QTimer::singleShot(0, this, SLOT(bail()));
196 }
197 else {
198 lastProfile = activeProfile = "default";
199 autoOpen = true;
200 QTimer::singleShot(0, this, SLOT(sessionStart()));
201 }
202 }
203 }
204}
205
206PsiMain::~PsiMain()
207{
208 delete pcon;
209
210#ifdef Q_OS_WIN
211 // remove Psi's settings from HKLM
212 QSettings *rs = new QSettings;
213 rs->setPath("Affinix", "psi", QSettings::Global);
214 rs->removeEntry("/lastProfile");
215 rs->removeEntry("/lastLang");
216 rs->removeEntry("/autoOpen");
217
218 QString affinixKey = "Software\\Affinix";
219#ifdef Q_OS_TEMP
220 RegDeleteKeyW(HKEY_LOCAL_MACHINE, affinixKey.ucs2());
221#else
222 RegDeleteKeyA(HKEY_LOCAL_MACHINE, affinixKey.latin1());
223#endif
224 delete rs;
225#endif
226
227 QSettings *s = new QSettings;
228 s->setPath("psi.affinix.com", "Psi", QSettings::User); // make Psi load all prefs from HKCU on Windows systems
229 s->insertSearchPath(QSettings::Windows, "/Affinix");
230 s->insertSearchPath(QSettings::Unix, g.pathHome);
231 s->writeEntry("/psi/lastProfile", lastProfile);
232 s->writeEntry("/psi/lastLang", lastLang);
233 s->writeEntry("/psi/autoOpen", autoOpen);
234 delete s;
235}
236
237static bool loadGlobal()
238{
239 // set the paths
240 g.pathBase = getResourcesDir();
241 char *p = getenv("PSIDATADIR");
242 if(p)
243 g.pathHome = p;
244 else
245 g.pathHome = getHomeDir();
246 g.pathProfiles = g.pathHome + "/profiles";
247
248 QDir d(g.pathProfiles);
249 if(!d.exists()) {
250 QDir d(g.pathHome);
251 d.mkdir("profiles");
252 }
253
254 return TRUE;
255}
256
257void PsiMain::chooseProfile()
258{
259 if(pcon) {
260 delete pcon;
261 pcon = 0;
262 }
263
264 QString str = "";
265
266 // dirty, dirty, dirty hack
267 is = new PsiIconset;
268 is->loadSystem();
269
270 while(1) {
271 ProfileOpenDlg *w = new ProfileOpenDlg(lastProfile, langs, curLang);
272 w->ck_auto->setChecked(autoOpen);
273 int r = w->exec();
274 // lang change
275 if(r == 10) {
276 QString newLang = w->newLang;
277 delete w;
278 setLang(newLang);
279 lastLang = curLang;
280 continue;
281 }
282 else {
283 if(r == QDialog::Accepted)
284 str = w->cb_profile->currentText();
285 autoOpen = w->ck_auto->isChecked();
286 delete w;
287 break;
288 }
289 }
290
291 delete is;
292
293 if(str.isEmpty()) {
294 quit();
295 return;
296 }
297
298 // only set lastProfile if the user opened it
299 lastProfile = str;
300
301 activeProfile = str;
302 sessionStart();
303}
304
305void PsiMain::sessionStart()
306{
307 // get a PsiCon
308 pcon = new PsiCon();
309 if(!pcon->init()) {
310 delete pcon;
311 pcon = 0;
312 quit();
313 return;
314 }
315 connect(pcon, SIGNAL(quit(int)), SLOT(sessionQuit(int)));
316}
317
318void PsiMain::sessionQuit(int x)
319{
320 if(x == PsiCon::QuitProgram) {
321 QTimer::singleShot(0, this, SLOT(bail()));
322 }
323 else if(x == PsiCon::QuitProfile) {
324 QTimer::singleShot(0, this, SLOT(chooseProfile()));
325 }
326}
327
328void PsiMain::bail()
329{
330 if(pcon) {
331 delete pcon;
332 pcon = 0;
333 }
334 quit();
335}
336
337int main(int argc, char *argv[])
338{
339 // add library paths before creating QApplication
340 if (!loadGlobal())
341 return 1;
342
343 QApplication::addLibraryPath(g.pathHome);
344 QApplication::addLibraryPath(getResourcesDir());
345
346 PsiApplication *app = new PsiApplication(argc, argv);
347
348#ifdef USE_CRASH
349 int useCrash = true;
350 int i;
351 for(i = 1; i < argc; ++i) {
352 QString str = argv[i];
353 if ( str == "--nocrash" )
354 useCrash = false;
355 }
356
357 if ( useCrash )
358 Crash::registerSigsegvHandler(argv[0]);
359#endif
360
361 // seed the random number generator
362 srand(time(NULL));
363
364 //dtcp_port = 8000;
365 for(int n = 1; n < argc; ++n) {
366 QString str = argv[n];
367 QString var, val;
368 int x = str.find('=');
369 if(x == -1) {
370 var = str;
371 val = "";
372 }
373 else {
374 var = str.mid(0,x);
375 val = str.mid(x+1);
376 }
377
378 if(var == "--no-gpg")
379 use_gpg = false;
380 else if(var == "--no-gpg-agent")
381 no_gpg_agent = true;
382 //else if(var == "--linktest")
383 // link_test = true;
384 }
385
386 //if(link_test)
387 // printf("Link test enabled\n");
388
389 // silly winsock workaround
390 //QSocketDevice *d = new QSocketDevice;
391 //delete d;
392
393 // japanese
394 trans = new QTranslator(0);
395 app->installTranslator(trans);
396
397 qttrans = new QTranslator(0);
398 app->installTranslator(qttrans);
399
400 // need SHA1 for Iconset sound
401 if(!QCA::isSupported(QCA::CAP_SHA1))
402 QCA::insertProvider(XMPP::createProviderHash());
403
404 PsiMain *psi = new PsiMain;
405 QObject::connect(psi, SIGNAL(quit()), app, SLOT(quit()));
406 app->exec();
407 delete psi;
408
409 app->removeTranslator(trans);
410 delete trans;
411 trans = 0;
412 app->removeTranslator(qttrans);
413 delete qttrans;
414 qttrans = 0;
415
416 delete app;
417
418 QCA::unloadAllPlugins();
419
420 return 0;
421}
Note: See TracBrowser for help on using the repository browser.