~ubuntu-branches/ubuntu/maverick/qgo/maverick

« back to all changes in this revision

Viewing changes to src/qgo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin A. Godisch
  • Date: 2005-01-01 23:07:10 UTC
  • Revision ID: james.westby@ubuntu.com-20050101230710-fhng6yidm47xlb2i
Tags: upstream-1.0.0-r2
ImportĀ upstreamĀ versionĀ 1.0.0-r2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* qgo.cpp
 
3
*/
 
4
 
 
5
#include "qgo.h"
 
6
#include "helpviewer.h"
 
7
#include "board.h"
 
8
#include "mainwindow.h"
 
9
#include "setting.h"
 
10
 
 
11
#include "config.h"
 
12
#include <qmessagebox.h>
 
13
#include <qsound.h>
 
14
#include <qdir.h>
 
15
#include <qmessagebox.h>
 
16
#include <qlineedit.h>
 
17
 
 
18
#ifdef Q_OS_MACX
 
19
#include <CoreFoundation/CFString.h>
 
20
#include <CoreFoundation/CFBundle.h>
 
21
#endif //Q_OS_MACX
 
22
 
 
23
qGo::qGo() : QObject()
 
24
{
 
25
        boardList = new QPtrList<MainWindow>;
 
26
        boardList->setAutoDelete(false);
 
27
        helpViewer = NULL;
 
28
        clickSound = NULL;
 
29
        talkSound = NULL;
 
30
        matchSound = NULL;
 
31
        passSound = NULL;
 
32
        gameEndSound = NULL;
 
33
        timeSound = NULL;
 
34
        saySound = NULL;
 
35
        enterSound = NULL;
 
36
        leaveSound = NULL;
 
37
        connectSound = NULL;
 
38
}
 
39
 
 
40
qGo::~qGo()
 
41
{
 
42
        boardList->clear();
 
43
        delete boardList;
 
44
        
 
45
        delete helpViewer;
 
46
        delete clickSound;
 
47
 
 
48
        //      settings = 0;
 
49
        boardList = 0;
 
50
        helpViewer = 0;
 
51
}
 
52
 
 
53
void qGo::quit()
 
54
{
 
55
        emit signal_leave_qgo();
 
56
        int check;
 
57
        if ((check = checkModified()) == 1 ||
 
58
                (!check &&
 
59
                !QMessageBox::warning(0, PACKAGE,
 
60
                tr("At least one board is modified.\n"
 
61
                "If you exit the application now, all changes will be lost!"
 
62
                "\nExit anyways?"),
 
63
                tr("Yes"), tr("No"), QString::null,
 
64
                1, 0)))
 
65
        {
 
66
                //      qApp->quit();
 
67
                qDebug("Program quits now...");
 
68
        }
 
69
        
 
70
        //    emit signal_leave_qgo();
 
71
}
 
72
 
 
73
MainWindow* qGo::addBoardWindow(MainWindow *w)
 
74
{
 
75
        if (w == NULL)
 
76
        {
 
77
                qWarning("*** BOARD HAS NO PARENT");
 
78
                w = new MainWindow(0, PACKAGE);
 
79
                w->getInterfaceHandler()->toggleMode();
 
80
                w->getInterfaceHandler()->toggleMode();
 
81
        }
 
82
        w->show();
 
83
        boardList->append(w);
 
84
        
 
85
        return w;
 
86
}
 
87
 
 
88
void qGo::removeBoardWindow(MainWindow *w)
 
89
{
 
90
        if (w == NULL)
 
91
        {
 
92
                qWarning("qGo::removeBoardWindow(QWidget *w) - paramter w is 0!");
 
93
                return;
 
94
        }
 
95
        
 
96
        if (!boardList->removeRef(w))
 
97
                qWarning("Failed to remove board from list.");
 
98
}
 
99
 
 
100
void qGo::openManual()
 
101
{
 
102
        if (helpViewer == NULL)
 
103
                helpViewer = new HelpViewer(0, "helpviewer");
 
104
        helpViewer->show();
 
105
        helpViewer->raise();
 
106
}
 
107
 
 
108
 
 
109
int qGo::checkModified()
 
110
{
 
111
        // Just closed the last board?
 
112
        if (boardList->isEmpty())
 
113
                return 1;
 
114
        
 
115
        // One board, same as closing a single window
 
116
        if (boardList->count() == 1)
 
117
                return ((MainWindow*)(boardList->first()))->checkModified(true);
 
118
    
 
119
        // Several boards. Check if one is modified.
 
120
        QWidget *b;
 
121
        for (b=boardList->first(); b != 0; b=boardList->next())
 
122
                if (!((MainWindow*)b)->checkModified(false))
 
123
                        return 0;
 
124
                
 
125
        return 1;
 
126
}
 
127
 
 
128
void qGo::updateAllBoardSettings()
 
129
{
 
130
        for (QWidget *w=boardList->first(); w != 0; w=boardList->next())
 
131
                ((MainWindow*)w)->updateBoard();
 
132
}
 
133
 
 
134
void qGo::updateFont()
 
135
{
 
136
        for (QWidget *w=boardList->first(); w != 0; w=boardList->next())
 
137
                ((MainWindow*)w)->updateFont();
 
138
 
 
139
        emit signal_updateFont();
 
140
}
 
141
 
 
142
bool qGo::testSound(bool showmsg)
 
143
{
 
144
        // qDebug("qGo::testSound()");
 
145
        
 
146
        // Sound system supported?
 
147
        if (!QSound::available())
 
148
        {
 
149
                if (showmsg)
 
150
                {
 
151
#ifdef Q_WS_WIN
 
152
                        QMessageBox::information(0, PACKAGE, tr("No sound available."));
 
153
                        clickSound = NULL;
 
154
                        return false;
 
155
#elif defined (Q_OS_MACX) 
 
156
                        QMessageBox::information(0, PACKAGE, tr("No sound available. Qt on Mac uses QuickTime sound."));
 
157
#else
 
158
                        QMessageBox::information(0, PACKAGE, tr("You are not running the Network Audio sytem.\n"
 
159
                                "If you have the `au' command, run it in the background before this program. The latest release of the Network Audio System can be obtained from:\n\n"
 
160
                                "ftp.ncd.com:/pub/ncd/technology/src/nas\n"
 
161
                                "ftp.x.org:/contrib/audio/nas\n\n"
 
162
                                "Release 1.2 of NAS is also included with the X11R6 contrib distribution. After installing NAS, you will then need to reconfigure Qt with NAS sound support.\n\n"
 
163
                                "Nevertheless, if you have oss, sound should be working and directed to /dev/dsp"));
 
164
#endif
 
165
                }
 
166
        }
 
167
        else if (showmsg)
 
168
        {
 
169
                QMessageBox::information(0, PACKAGE, tr("Sound available."));
 
170
                return true;
 
171
        }
 
172
        
 
173
        //    qDebug("Sound available, checking for sound files...");
 
174
 
 
175
        // Sound files found?
 
176
        QStringList list;
 
177
 
 
178
        ASSERT(setting->program_dir);
 
179
 
 
180
#ifdef Q_WS_WIN
 
181
        list << applicationPath + "/sounds"
 
182
                << setting->program_dir + "/sounds"
 
183
                << "C:/Program Files/qGo/sounds"
 
184
                << "D:/Program Files/qGo/sounds"
 
185
                << "E:/Program Files/qGo/sounds"
 
186
                << "C:/Programme/qGo/sounds"
 
187
                << "D:/Programme/qGo/sounds"
 
188
                << "E:/Programme/qGo/sounds"
 
189
                << "./sounds";
 
190
#elif defined(Q_OS_MACX)
 
191
        //get the bundle path and find our resources like sounds
 
192
        CFURLRef bundleRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
 
193
        CFStringRef bundlePath = CFURLCopyFileSystemPath(bundleRef, kCFURLPOSIXPathStyle);
 
194
        list << (QString)CFStringGetCStringPtr(bundlePath, CFStringGetSystemEncoding())         
 
195
                + "/Contents/Resources";
 
196
#else
 
197
        list << setting->program_dir + "/sounds"
 
198
                << "./share/" PACKAGE "/sounds"
 
199
                << "/usr/share/" PACKAGE "/sounds"
 
200
                << "/usr/local/share/" PACKAGE "/sounds"
 
201
                << "/sounds"
 
202
                << "./sounds"
 
203
                << "./src/sounds";                           //SL added eb 7
 
204
#endif
 
205
 
 
206
        for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
 
207
        {
 
208
                QDir d(*it);
 
209
                if (d.exists())
 
210
                {
 
211
                        QString s = (QString)(*it);
 
212
                        s.append("/stone.wav");
 
213
                        if (QFile::exists(s))
 
214
                                clickSound = new QSound(s);
 
215
                        else
 
216
                                qDebug("stone.wav not found...");
 
217
 
 
218
                        s = (QString)(*it);
 
219
                        s.append("/talk.wav");
 
220
                        if (QFile::exists(s))
 
221
                                talkSound = new QSound(s);
 
222
                        else
 
223
                                qDebug("talk.wav not found...");
 
224
 
 
225
                        s = (QString)(*it);
 
226
                        s.append("/match.wav");
 
227
                        if (QFile::exists(s))
 
228
                                matchSound = new QSound(s);
 
229
                        else
 
230
                                qDebug("match.wav not found...");
 
231
 
 
232
                        s = (QString)(*it);
 
233
                        s.append("/pass.wav");
 
234
                        if (QFile::exists(s))
 
235
                                passSound = new QSound(s);
 
236
                        else
 
237
                                qDebug("pass.wav not found...");
 
238
 
 
239
                        s = (QString)(*it);
 
240
                        s.append("/gameend.wav");
 
241
                        if (QFile::exists(s))
 
242
                                gameEndSound = new QSound(s);
 
243
                        else
 
244
                                qDebug("gameend.wav not found...");
 
245
 
 
246
                        s = (QString)(*it);
 
247
                        s.append("/tictoc.wav");
 
248
                        if (QFile::exists(s))
 
249
                                timeSound = new QSound(s);
 
250
                        else
 
251
                                qDebug("tictoc.wav not found...");
 
252
 
 
253
                        s = (QString)(*it);
 
254
                        s.append("/say.wav");
 
255
                        if (QFile::exists(s))
 
256
                                saySound = new QSound(s);
 
257
                        else
 
258
                                qDebug("say.wav not found...");
 
259
                        
 
260
                        s = (QString)(*it);
 
261
                        s.append("/enter.wav");
 
262
                        if (QFile::exists(s))
 
263
                                enterSound = new QSound(s);
 
264
                        else
 
265
                                qDebug("enter.wav not found...");
 
266
                        
 
267
                        s = (QString)(*it);
 
268
                        s.append("/leave.wav");
 
269
                        if (QFile::exists(s))
 
270
                                leaveSound = new QSound(s);
 
271
                        else
 
272
                                qDebug("leave.wav not found...");
 
273
                        
 
274
                        s = (QString)(*it);
 
275
                        s.append("/connect.wav");
 
276
                        if (QFile::exists(s))
 
277
                                connectSound = new QSound(s);
 
278
                        else
 
279
                                qDebug("connect.wav not found...");
 
280
                        
 
281
                        // QMessageBox::information(0, "SOUND OK", s);
 
282
                        //          qDebug("Sound Ok. Soundfile: %s", s.latin1());
 
283
#ifdef Q_WS_WIN
 
284
                        if (!applicationPath)
 
285
                                applicationPath = s.left(s.find("/sounds"));
 
286
                        // QMessageBox::information(0, "SAVING", applicationPath);
 
287
#endif
 
288
                        return true;
 
289
                }
 
290
        }
 
291
 
 
292
#ifdef Q_OS_MACX
 
293
        QMessageBox::information(0, PACKAGE, tr("No sound files in bundle, strange.\n"));
 
294
#elif ! defined(Q_WS_WIN)
 
295
        QMessageBox::information(0, PACKAGE, tr("Sound files not found.") + "\n" +
 
296
                tr("Please check for the directories") + " /usr/local/share/" + PACKAGE + "/sounds/ " + tr("or") +
 
297
                " /usr/share/" + PACKAGE + "/sounds/, " + tr("depending on your installation."));
 
298
#else
 
299
 
 
300
        if (applicationPath = setting->readEntry("PATH_SOUND"))
 
301
                return testSound(false);
 
302
 
 
303
        QMessageBox::information(0, PACKAGE, tr("Sound files not found.") + "\n" +
 
304
                             tr("You can navigate to the main qGo directory (for example:") + " C:\\Program Files\\" + PACKAGE + " .\n" +
 
305
                                 tr("If the directory was given correctly, this data will be saved and you won't"
 
306
                                 "be asked\nanymore except you install qGo again into a different directory.\n"
 
307
                                 "To abort this procedure, click 'Cancel' in the following dialog."));
 
308
    
 
309
        applicationPath = QFileDialog::getExistingDirectory(NULL, NULL, "appdir", tr("qGo directory"), true);
 
310
        
 
311
        if (applicationPath.isNull() || applicationPath.isEmpty())
 
312
        {
 
313
                QMessageBox::warning(0, PACKAGE, tr("No valid directory was given. Sound is not available."));
 
314
                return false;
 
315
        }
 
316
 
 
317
        // save path
 
318
        setting->writeEntry("PATH_SOUND", applicationPath);
 
319
 
 
320
        // QMessageBox::information(0, "TRYING AGAIN", applicationPath);
 
321
        return testSound(false);
 
322
#endif
 
323
        return false;
 
324
}
 
325
 
 
326
void qGo::playClick()
 
327
{
 
328
        if (setting->readBoolEntry("SOUND_STONE") && clickSound)
 
329
        {                                                                      //added eb 7
 
330
#ifdef Q_OS_LINUX
 
331
                if (!clickSound->isAvailable())
 
332
                        play(clickSound->fileName().latin1());
 
333
                else
 
334
#endif
 
335
                        clickSound->play();
 
336
        }                                                                      //end add eb 7
 
337
}
 
338
 
 
339
void qGo::playAutoPlayClick()
 
340
{
 
341
        if (setting->readBoolEntry("SOUND_AUTOPLAY") && clickSound)
 
342
        {                                                                      //added eb 7
 
343
#ifdef Q_OS_LINUX
 
344
                if (!clickSound->isAvailable())
 
345
                        play(clickSound->fileName().latin1());
 
346
                else
 
347
#endif
 
348
                        clickSound->play();
 
349
        }                                                                      //end add eb 7
 
350
}
 
351
 
 
352
void qGo::playTalkSound()
 
353
{
 
354
        if (setting->readBoolEntry("SOUND_TALK") && talkSound)
 
355
        {                                                                      //added eb 7
 
356
#ifdef Q_OS_LINUX
 
357
                if (!talkSound->isAvailable())
 
358
                        play(talkSound->fileName().latin1());
 
359
                else
 
360
#endif
 
361
                        talkSound->play();
 
362
        }                                                                      //end add eb 7
 
363
}
 
364
 
 
365
void qGo::playMatchSound()
 
366
{
 
367
        if (setting->readBoolEntry("SOUND_MATCH") && matchSound)
 
368
        {                                                                      //added eb 7
 
369
#ifdef Q_OS_LINUX
 
370
                if (!matchSound->isAvailable())
 
371
                        play(matchSound->fileName().latin1());
 
372
                else
 
373
#endif
 
374
                        matchSound->play();
 
375
        }                                                                      //end add eb 7
 
376
}
 
377
 
 
378
void qGo::playPassSound()
 
379
{
 
380
        if (setting->readBoolEntry("SOUND_PASS") && passSound)
 
381
        {                                                                      //added eb 7
 
382
#ifdef Q_OS_LINUX
 
383
                if (!passSound->isAvailable())
 
384
                        play(passSound->fileName().latin1());
 
385
                else
 
386
#endif
 
387
                        passSound->play();
 
388
        }                                                                      //end add eb 7
 
389
}
 
390
 
 
391
void qGo::playGameEndSound()
 
392
{
 
393
        if (setting->readBoolEntry("SOUND_GAMEEND") && gameEndSound)
 
394
        {                                                                      //added eb 7
 
395
#ifdef Q_OS_LINUX
 
396
                if (!gameEndSound->isAvailable())
 
397
                        play(gameEndSound->fileName().latin1());
 
398
                else
 
399
#endif
 
400
                        gameEndSound->play();
 
401
        }                                                                      //end add eb 7
 
402
}
 
403
 
 
404
void qGo::playTimeSound()
 
405
{
 
406
        if (setting->readBoolEntry("SOUND_TIME") && timeSound)
 
407
        {                                                                      //added eb 7
 
408
#ifdef Q_OS_LINUX
 
409
                if (!timeSound->isAvailable())
 
410
                        play(timeSound->fileName().latin1());
 
411
                else
 
412
#endif
 
413
                        timeSound->play();
 
414
        }                                                                      //end add eb 7
 
415
}
 
416
 
 
417
void qGo::playSaySound()
 
418
{
 
419
        if (setting->readBoolEntry("SOUND_SAY") && saySound)
 
420
        {                                                                      //added eb 7
 
421
#ifdef Q_OS_LINUX
 
422
                if (!saySound->isAvailable())
 
423
                        play(saySound->fileName().latin1());
 
424
                else
 
425
#endif
 
426
                        saySound->play();
 
427
        }                                                                      //end add eb 7
 
428
}
 
429
 
 
430
void qGo::playEnterSound()
 
431
{
 
432
        if (setting->readBoolEntry("SOUND_ENTER") && enterSound)
 
433
        {                                                                      //added eb 7
 
434
#ifdef Q_OS_LINUX
 
435
                if (!enterSound->isAvailable())
 
436
                        play(enterSound->fileName().latin1());
 
437
                else
 
438
#endif
 
439
                        enterSound->play();
 
440
        }                                                                      //end add eb 7
 
441
}
 
442
 
 
443
void qGo::playLeaveSound()
 
444
{
 
445
        if (setting->readBoolEntry("SOUND_LEAVE") && leaveSound)
 
446
        {                                                                      //added eb 7
 
447
#ifdef Q_OS_LINUX
 
448
                if (!leaveSound->isAvailable())
 
449
                        play(leaveSound->fileName().latin1());
 
450
                else
 
451
#endif
 
452
                        leaveSound->play();
 
453
        }                                                                      //end add eb 7
 
454
}
 
455
 
 
456
void qGo::playConnectSound()
 
457
{
 
458
 
 
459
 
 
460
        if (setting->readBoolEntry("SOUND_CONNECT") && connectSound)
 
461
        {                                                                      //added eb 7
 
462
#ifdef Q_OS_LINUX
 
463
                if (!connectSound->isAvailable())
 
464
                        play(connectSound->fileName().latin1());
 
465
                else
 
466
#endif
 
467
                        connectSound->play();
 
468
        }                                                                      //end add eb 7
 
469
}
 
470
 
 
471
void qGo::playDisConnectSound()
 
472
{
 
473
        if (setting->readBoolEntry("SOUND_DISCONNECT") && connectSound)
 
474
        {                                                                      //added eb 7
 
475
#ifdef Q_OS_LINUX
 
476
                if (!connectSound->isAvailable())
 
477
                        play(connectSound->fileName().latin1());
 
478
                else
 
479
#endif
 
480
                        connectSound->play();
 
481
        }                                                                      //end add eb 7
 
482
}
 
483
 
 
484
void qGo::slotHelpAbout()
 
485
{
 
486
        QString txt = PACKAGE " " VERSION
 
487
                "\n\nCopyright (c) 2001-2004\nPeter Strempel <pstrempel@t-online.de>\nJohannes Mesa <frosla@gmx.at>\nEmmanuel Beranger <e2@beranger.com.fr>\n\n" +
 
488
                tr("GTP code from Goliath, thanks to:") + "\nPALM Thomas\nDINTILHAC Florian\nHIVERT Anthony\nPIOC Sebastien";
 
489
        
 
490
        QString translation = tr("English translation by:\nPeter Strempel\nJohannes Mesa\nEmmanuel Beranger", "Please set your own language and your name! Use your own language!");
 
491
        if (translation != "English translation by:\nPeter Strempel\nJohannes Mesa\nEmmanuel Beranger")
 
492
                txt += "\n\n" + translation;
 
493
        
 
494
        QMessageBox::about(0, tr("About..."), txt);
 
495
}