Memory Management Improved
[pierogi] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3
4 #include <QtCore/QCoreApplication>
5 #include <QMutex>
6 #include <QtGui/QMessageBox>
7 #include <QSettings>
8 #include <QMaemo5InformationBox>
9
10 #include "pirkeysetwidgetitem.h"
11 #include "pirselectkeysetform.h"
12 #include "pirsecondaryform.h"
13 #include "pirdocumentationform.h"
14 #include "piraboutform.h"
15 #include "pirkeysetmanager.h"
16
17 //#define DEBUGGING
18
19 // Some ugly globals used for thread communications:
20
21 // A global to show that a command is being processed:
22 bool commandInFlight = false;
23 QMutex commandIFMutex;
24
25 // The stopRepeatingFlag boolean is the method used to tell running commands
26 // in the worker thread to stop:
27 bool stopRepeatingFlag = false;
28 QMutex stopRepeatingMutex;
29
30 extern PIRMakeMgr makeManager;
31
32
33 MainWindow::MainWindow(QWidget *parent)
34   : QMainWindow(parent),
35     ui(new Ui::MainWindow),
36     selectKeysetForm(0),
37     secondaryForm(0),
38     documentationForm(0),
39     aboutForm(0),
40     currentKeyset(0)
41 {
42   ui->setupUi(this);
43
44   // Make this a Maemo 5 stacked widget:
45   setAttribute(Qt::WA_Maemo5StackedWindow);
46
47   // Collect the keysets:
48   myKeysets = new PIRKeysetManager();
49
50   // Set up the keyset selection window:
51   selectKeysetForm = new PIRSelectKeysetForm(this);
52
53   // Set up the secondary buttons window:
54   secondaryForm = new PIRSecondaryForm(this);
55
56   myKeysets->populateGuiWidget(selectKeysetForm);
57
58   // Remember any favorites the user has already set:
59   populateFavorites();
60
61   QSettings settings("pietrzak.org", "Pierogi");
62   if (settings.contains("currentKeysetName"))
63   {
64     myKeysets->findKeysetID(
65       settings.value("currentKeysetMake").toString(),
66       settings.value("currentKeysetName").toString(),
67       currentKeyset);
68 //    currentKeyset = settings.value("currentKeyset").toInt();
69   }
70
71   enableButtons();
72
73   connect(
74     ui->favoriteKeysetsWidget,
75 //    SIGNAL(itemActivated(QListWidgetItem *)),
76     SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
77     this,
78     SLOT(keysetSelectionChanged(QListWidgetItem *)),
79     Qt::QueuedConnection);
80
81   // Make sure the two selection lists don't show different selections:
82   QListWidget *klw = selectKeysetForm->getKeysetListWidget();
83   connect(
84     ui->favoriteKeysetsWidget,
85     SIGNAL(itemActivated(QListWidgetItem *)),
86     klw,
87     SLOT(clearSelection()),
88     Qt::QueuedConnection);
89
90   connect(
91     klw,
92     SIGNAL(itemActivated(QListWidgetItem *)),
93     ui->favoriteKeysetsWidget,
94     SLOT(clearSelection()),
95     Qt::QueuedConnection);
96
97 #ifndef DEBUGGING
98   // The PIRModprobe object should take care of setting up and shutting down
99   // the lirc_rx51 kernel module, if necessary:
100  
101   if (modprobeObj.loadRX51Module() != 0)
102   {
103     // Couldn't load module, quit:
104     QMessageBox errBox;
105     errBox.setText("Couldn't load lirc_rx51 kernel module!");
106     errBox.setIcon(QMessageBox::Warning);
107     errBox.exec();
108 //    throw; // Need a clean way to exit here!!!
109   }
110 #endif
111 }
112
113
114 MainWindow::~MainWindow()
115 {
116   delete myKeysets;
117   if (selectKeysetForm) delete selectKeysetForm;
118   if (documentationForm) delete documentationForm;
119   if (aboutForm) delete aboutForm;
120   delete ui;
121 }
122
123
124 void MainWindow::setOrientation(ScreenOrientation orientation)
125 {
126 #if defined(Q_OS_SYMBIAN)
127     // If the version of Qt on the device is < 4.7.2, that attribute won't work
128     if (orientation != ScreenOrientationAuto) {
129         const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
130         if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
131             qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
132             return;
133         }
134     }
135 #endif // Q_OS_SYMBIAN
136
137     Qt::WidgetAttribute attribute;
138     switch (orientation) {
139 #if QT_VERSION < 0x040702
140     // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
141     case ScreenOrientationLockPortrait:
142         attribute = static_cast<Qt::WidgetAttribute>(128);
143         break;
144     case ScreenOrientationLockLandscape:
145         attribute = static_cast<Qt::WidgetAttribute>(129);
146         break;
147     default:
148     case ScreenOrientationAuto:
149         attribute = static_cast<Qt::WidgetAttribute>(130);
150         break;
151 #else // QT_VERSION < 0x040702
152     case ScreenOrientationLockPortrait:
153         attribute = Qt::WA_LockPortraitOrientation;
154         break;
155     case ScreenOrientationLockLandscape:
156         attribute = Qt::WA_LockLandscapeOrientation;
157         break;
158     default:
159     case ScreenOrientationAuto:
160         attribute = Qt::WA_AutoOrientation;
161         break;
162 #endif // QT_VERSION < 0x040702
163     };
164     setAttribute(attribute, true);
165 }
166
167 void MainWindow::showExpanded()
168 {
169 #if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
170     showFullScreen();
171 #elif defined(Q_WS_MAEMO_5)
172     showMaximized();
173 #else
174     show();
175 #endif
176 }
177
178
179 void MainWindow::enableButtons()
180 {
181   // Just to be sure, check to see if the keyset has been populated:
182   myKeysets->populateKeyset(this, currentKeyset);
183
184   // This is going to be a little painful...
185   // Main keys
186   emit powerEnabled(myKeysets->hasKey(currentKeyset, Power_Key));
187   emit volumeUpEnabled(myKeysets->hasKey(currentKeyset, VolumeUp_Key));
188   emit volumeDownEnabled(myKeysets->hasKey(currentKeyset, VolumeDown_Key));
189   emit channelUpEnabled(myKeysets->hasKey(currentKeyset, ChannelUp_Key));
190   emit channelDownEnabled(myKeysets->hasKey(currentKeyset, ChannelDown_Key));
191   emit muteEnabled(myKeysets->hasKey(currentKeyset, Mute_Key));
192
193   // Main tab labels:
194   emit keysetMakeChanged(
195     makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
196   emit keysetNameChanged(myKeysets->getDisplayName(currentKeyset));
197
198   // Utility keys:
199   emit redEnabled(myKeysets->hasKey(currentKeyset, Red_Key));
200   emit greenEnabled(myKeysets->hasKey(currentKeyset, Green_Key));
201   emit yellowEnabled(myKeysets->hasKey(currentKeyset, Yellow_Key));
202   emit blueEnabled(myKeysets->hasKey(currentKeyset, Blue_Key));
203   emit pictureModeEnabled(myKeysets->hasKey(currentKeyset, PictureMode_Key));
204   emit soundModeEnabled(myKeysets->hasKey(currentKeyset, SoundMode_Key));
205   emit aspectRatioEnabled(myKeysets->hasKey(currentKeyset, AspectRatio_Key));
206 //  emit surroundEnabled(myKeysets->hasKey(currentKeyset, Surround_Key));
207   emit audioEnabled(myKeysets->hasKey(currentKeyset, Audio_Key));
208   emit infoEnabled(myKeysets->hasKey(currentKeyset, Info_Key));
209   emit captionsEnabled(myKeysets->hasKey(currentKeyset, Captions_Key));
210   emit sleepEnabled(myKeysets->hasKey(currentKeyset, Sleep_Key));
211   emit inputEnabled(myKeysets->hasKey(currentKeyset, Input_Key));
212
213   // Keypad keys
214   emit zeroEnabled(myKeysets->hasKey(currentKeyset, Zero_Key));
215   emit oneEnabled(myKeysets->hasKey(currentKeyset, One_Key));
216   emit twoEnabled(myKeysets->hasKey(currentKeyset, Two_Key));
217   emit threeEnabled(myKeysets->hasKey(currentKeyset, Three_Key));
218   emit fourEnabled(myKeysets->hasKey(currentKeyset, Four_Key));
219   emit fiveEnabled(myKeysets->hasKey(currentKeyset, Five_Key));
220   emit sixEnabled(myKeysets->hasKey(currentKeyset, Six_Key));
221   emit sevenEnabled(myKeysets->hasKey(currentKeyset, Seven_Key));
222   emit eightEnabled(myKeysets->hasKey(currentKeyset, Eight_Key));
223   emit nineEnabled(myKeysets->hasKey(currentKeyset, Nine_Key));
224   emit enterEnabled(myKeysets->hasKey(currentKeyset, Enter_Key));
225   emit clearEnabled(myKeysets->hasKey(currentKeyset, Clear_Key));
226   emit dashEnabled(myKeysets->hasKey(currentKeyset, Dash_Key));
227   emit plusOneHundredEnabled(myKeysets->hasKey(currentKeyset, PlusOneHundred_Key));
228   emit doubleDigitEnabled(myKeysets->hasKey(currentKeyset, DoubleDigit_Key));
229   emit prevChannelEnabled(myKeysets->hasKey(currentKeyset, PrevChannel_Key));
230
231   // Menu keys:
232   emit upEnabled(myKeysets->hasKey(currentKeyset, Up_Key));
233   emit downEnabled(myKeysets->hasKey(currentKeyset, Down_Key));
234   emit leftEnabled(myKeysets->hasKey(currentKeyset, Left_Key));
235   emit rightEnabled(myKeysets->hasKey(currentKeyset, Right_Key));
236   emit selectEnabled(myKeysets->hasKey(currentKeyset, Select_Key));
237   emit menuEnabled(myKeysets->hasKey(currentKeyset, Menu_Key));
238   emit exitEnabled(myKeysets->hasKey(currentKeyset, Exit_Key));
239   emit guideEnabled(myKeysets->hasKey(currentKeyset, Guide_Key));
240   emit discMenuEnabled(myKeysets->hasKey(currentKeyset, DiscMenu_Key));
241
242   // Media keys:
243   emit nextEnabled(myKeysets->hasKey(currentKeyset, Next_Key));
244   emit previousEnabled(myKeysets->hasKey(currentKeyset, Previous_Key));
245   emit advanceEnabled(myKeysets->hasKey(currentKeyset, Advance_Key));
246   emit replayEnabled(myKeysets->hasKey(currentKeyset, Replay_Key));
247   emit stepForwardEnabled(myKeysets->hasKey(currentKeyset, StepForward_Key));
248   emit stepBackEnabled(myKeysets->hasKey(currentKeyset, StepBack_Key));
249   emit fastForwardEnabled(myKeysets->hasKey(currentKeyset, FastForward_Key));
250   emit reverseEnabled(myKeysets->hasKey(currentKeyset, Rewind_Key));
251   emit playEnabled(myKeysets->hasKey(currentKeyset, Play_Key));
252   emit pauseEnabled(myKeysets->hasKey(currentKeyset, Pause_Key));
253   emit stopEnabled(myKeysets->hasKey(currentKeyset, Stop_Key));
254   emit ejectEnabled(myKeysets->hasKey(currentKeyset, Eject_Key));
255
256   // Also enable the buttons on the secondary form:
257   secondaryForm->enableButtons(myKeysets, currentKeyset);
258 }
259
260
261 void MainWindow::receivedExternalWarning(
262   const char *warning)
263 {
264   QMessageBox errBox;
265   errBox.setText(warning);
266   errBox.setIcon(QMessageBox::Warning);
267   errBox.exec();
268 }
269
270
271 // Main tab buttons:
272
273 void MainWindow::on_powerButton_pressed()
274 {
275   startRepeating(Power_Key);
276 }
277
278 void MainWindow::on_powerButton_released()
279 {
280   stopRepeating();
281 }
282
283 void MainWindow::on_mainChannelUpButton_pressed()
284 {
285   startRepeating(ChannelUp_Key);
286 }
287
288 void MainWindow::on_mainChannelUpButton_released()
289 {
290   stopRepeating();
291 }
292
293 void MainWindow::on_mainChannelDownButton_pressed()
294 {
295   startRepeating(ChannelDown_Key);
296 }
297
298 void MainWindow::on_mainChannelDownButton_released()
299 {
300   stopRepeating();
301 }
302
303 void MainWindow::on_mainVolumeUp_pressed()
304 {
305   startRepeating(VolumeUp_Key);
306 }
307
308 void MainWindow::on_mainVolumeUp_released()
309 {
310   stopRepeating();
311 }
312
313 void MainWindow::on_mainVolumeDownButton_pressed()
314 {
315   startRepeating(VolumeDown_Key);
316 }
317
318 void MainWindow::on_mainVolumeDownButton_released()
319 {
320   stopRepeating();
321 }
322
323 void MainWindow::on_muteButton_pressed()
324 {
325   startRepeating(Mute_Key);
326 }
327
328 void MainWindow::on_muteButton_released()
329 {
330   stopRepeating();
331 }
332
333
334 // Utility tab buttons:
335
336 void MainWindow::on_redButton_pressed()
337 {
338   startRepeating(Red_Key);
339 }
340
341 void MainWindow::on_redButton_released()
342 {
343   stopRepeating();
344 }
345
346 void MainWindow::on_greenButton_pressed()
347 {
348   startRepeating(Green_Key);
349 }
350
351 void MainWindow::on_greenButton_released()
352 {
353   stopRepeating();
354 }
355
356 void MainWindow::on_yellowButton_pressed()
357 {
358   startRepeating(Yellow_Key);
359 }
360
361 void MainWindow::on_yellowButton_released()
362 {
363   stopRepeating();
364 }
365
366 void MainWindow::on_blueButton_pressed()
367 {
368   startRepeating(Blue_Key);
369 }
370
371 void MainWindow::on_blueButton_released()
372 {
373   stopRepeating();
374 }
375
376 void MainWindow::on_pictureModeButton_pressed()
377 {
378   startRepeating(PictureMode_Key);
379 }
380
381 void MainWindow::on_pictureModeButton_released()
382 {
383   stopRepeating();
384 }
385
386 void MainWindow::on_soundModeButton_pressed()
387 {
388   startRepeating(SoundMode_Key);
389 }
390
391 void MainWindow::on_soundModeButton_released()
392 {
393   stopRepeating();
394 }
395
396 void MainWindow::on_aspectRatioButton_pressed()
397 {
398   startRepeating(AspectRatio_Key);
399 }
400
401 void MainWindow::on_aspectRatioButton_released()
402 {
403   stopRepeating();
404 }
405
406 /*
407 void MainWindow::on_surroundButton_pressed()
408 {
409   startRepeating(Surround_Key);
410 }
411
412 void MainWindow::on_surroundButton_released()
413 {
414   stopRepeating();
415 }
416 */
417
418 void MainWindow::on_audioButton_pressed()
419 {
420   startRepeating(Audio_Key);
421 }
422
423 void MainWindow::on_audioButton_released()
424 {
425   stopRepeating();
426 }
427
428 void MainWindow::on_infoButton_pressed()
429 {
430   startRepeating(Info_Key);
431 }
432
433 void MainWindow::on_infoButton_released()
434 {
435   stopRepeating();
436 }
437
438 void MainWindow::on_captionButton_pressed()
439 {
440   startRepeating(Captions_Key);
441 }
442
443 void MainWindow::on_captionButton_released()
444 {
445   stopRepeating();
446 }
447
448 void MainWindow::on_inputButton_pressed()
449 {
450   startRepeating(Input_Key);
451 }
452
453 void MainWindow::on_inputButton_released()
454 {
455   stopRepeating();
456 }
457
458 void MainWindow::on_sleepButton_pressed()
459 {
460   startRepeating(Sleep_Key);
461 }
462
463 void MainWindow::on_sleepButton_released()
464 {
465   stopRepeating();
466 }
467
468
469 // Keypad tab buttons:
470
471 void MainWindow::on_oneButton_pressed()
472 {
473   startRepeating(One_Key);
474 }
475
476 void MainWindow::on_oneButton_released()
477 {
478   stopRepeating();
479 }
480
481 void MainWindow::on_twoButton_pressed()
482 {
483   startRepeating(Two_Key);
484 }
485
486 void MainWindow::on_twoButton_released()
487 {
488   stopRepeating();
489 }
490
491 void MainWindow::on_threeButton_pressed()
492 {
493   startRepeating(Three_Key);
494 }
495
496 void MainWindow::on_threeButton_released()
497 {
498   stopRepeating();
499 }
500
501 void MainWindow::on_fourButton_pressed()
502 {
503   startRepeating(Four_Key);
504 }
505
506 void MainWindow::on_fourButton_released()
507 {
508   stopRepeating();
509 }
510
511 void MainWindow::on_fiveButton_pressed()
512 {
513   startRepeating(Five_Key);
514 }
515
516 void MainWindow::on_fiveButton_released()
517 {
518   stopRepeating();
519 }
520
521 void MainWindow::on_sixButton_pressed()
522 {
523   startRepeating(Six_Key);
524 }
525
526 void MainWindow::on_sixButton_released()
527 {
528   stopRepeating();
529 }
530
531 void MainWindow::on_sevenButton_pressed()
532 {
533   startRepeating(Seven_Key);
534 }
535
536 void MainWindow::on_sevenButton_released()
537 {
538   stopRepeating();
539 }
540
541 void MainWindow::on_eightButton_pressed()
542 {
543   startRepeating(Eight_Key);
544 }
545
546 void MainWindow::on_eightButton_released()
547 {
548   stopRepeating();
549 }
550
551 void MainWindow::on_nineButton_pressed()
552 {
553   startRepeating(Nine_Key);
554 }
555
556 void MainWindow::on_nineButton_released()
557 {
558   stopRepeating();
559 }
560
561 void MainWindow::on_zeroButton_pressed()
562 {
563   startRepeating(Zero_Key);
564 }
565
566 void MainWindow::on_zeroButton_released()
567 {
568   stopRepeating();
569 }
570
571 void MainWindow::on_enterButton_pressed()
572 {
573   startRepeating(Enter_Key);
574 }
575
576 void MainWindow::on_enterButton_released()
577 {
578   stopRepeating();
579 }
580
581 void MainWindow::on_clearButton_pressed()
582 {
583   startRepeating(Clear_Key);
584 }
585
586 void MainWindow::on_clearButton_released()
587 {
588   stopRepeating();
589 }
590
591 void MainWindow::on_prevChannelButton_pressed()
592 {
593   startRepeating(PrevChannel_Key);
594 }
595
596 void MainWindow::on_prevChannelButton_released()
597 {
598   stopRepeating();
599 }
600
601 void MainWindow::on_plusOneHundredButton_pressed()
602 {
603   startRepeating(PlusOneHundred_Key);
604 }
605
606 void MainWindow::on_plusOneHundredButton_released()
607 {
608   stopRepeating();
609 }
610
611 void MainWindow::on_dashButton_pressed()
612 {
613   startRepeating(Dash_Key);
614 }
615
616 void MainWindow::on_dashButton_released()
617 {
618   stopRepeating();
619 }
620
621 void MainWindow::on_doubleDigitButton_pressed()
622 {
623   startRepeating(DoubleDigit_Key);
624 }
625
626 void MainWindow::on_doubleDigitButton_released()
627 {
628   stopRepeating();
629 }
630
631
632 // Menu tab buttons:
633
634 void MainWindow::on_upButton_pressed()
635 {
636   startRepeating(Up_Key);
637 }
638
639 void MainWindow::on_upButton_released()
640 {
641   stopRepeating();
642 }
643
644 void MainWindow::on_leftButton_pressed()
645 {
646   startRepeating(Left_Key);
647 }
648
649 void MainWindow::on_leftButton_released()
650 {
651   stopRepeating();
652 }
653
654 void MainWindow::on_rightButton_pressed()
655 {
656   startRepeating(Right_Key);
657 }
658
659 void MainWindow::on_rightButton_released()
660 {
661   stopRepeating();
662 }
663
664 void MainWindow::on_downButton_pressed()
665 {
666   startRepeating(Down_Key);
667 }
668
669 void MainWindow::on_downButton_released()
670 {
671   stopRepeating();
672 }
673
674 void MainWindow::on_selectButton_pressed()
675 {
676   startRepeating(Select_Key);
677 }
678
679 void MainWindow::on_selectButton_released()
680 {
681   stopRepeating();
682 }
683
684 void MainWindow::on_menuButton_pressed()
685 {
686   startRepeating(Menu_Key);
687 }
688
689 void MainWindow::on_menuButton_released()
690 {
691   stopRepeating();
692 }
693
694 void MainWindow::on_exitButton_pressed()
695 {
696   startRepeating(Exit_Key);
697 }
698
699 void MainWindow::on_exitButton_released()
700 {
701   stopRepeating();
702 }
703
704 void MainWindow::on_guideButton_pressed()
705 {
706   startRepeating(Guide_Key);
707 }
708
709 void MainWindow::on_guideButton_released()
710 {
711   stopRepeating();
712 }
713
714 void MainWindow::on_discMenuButton_pressed()
715 {
716   startRepeating(DiscMenu_Key);
717 }
718
719 void MainWindow::on_discMenuButton_released()
720 {
721   stopRepeating();
722 }
723
724
725 // Media tab buttons:
726
727 void MainWindow::on_mediaPreviousButton_pressed()
728 {
729   startRepeating(Previous_Key);
730 }
731
732 void MainWindow::on_mediaPreviousButton_released()
733 {
734   stopRepeating();
735 }
736
737 void MainWindow::on_mediaNextButton_pressed()
738 {
739   startRepeating(Next_Key);
740 }
741
742 void MainWindow::on_mediaNextButton_released()
743 {
744   stopRepeating();
745 }
746
747 void MainWindow::on_replayButton_pressed()
748 {
749   startRepeating(Replay_Key);
750 }
751
752 void MainWindow::on_replayButton_released()
753 {
754   stopRepeating();
755 }
756
757 void MainWindow::on_advanceButton_pressed()
758 {
759   startRepeating(Advance_Key);
760 }
761
762 void MainWindow::on_advanceButton_released()
763 {
764   stopRepeating();
765 }
766
767 void MainWindow::on_stepBackButton_pressed()
768 {
769   startRepeating(StepBack_Key);
770 }
771
772 void MainWindow::on_stepBackButton_released()
773 {
774   stopRepeating();
775 }
776
777 void MainWindow::on_stepForwardButton_pressed()
778 {
779   startRepeating(StepForward_Key);
780 }
781
782 void MainWindow::on_stepForwardButton_released()
783 {
784   stopRepeating();
785 }
786
787 void MainWindow::on_reverseButton_pressed()
788 {
789   startRepeating(Rewind_Key);
790 }
791
792 void MainWindow::on_reverseButton_released()
793 {
794   stopRepeating();
795 }
796
797 void MainWindow::on_fastForwardButton_pressed()
798 {
799   startRepeating(FastForward_Key);
800 }
801
802 void MainWindow::on_fastForwardButton_released()
803 {
804   stopRepeating();
805 }
806
807 void MainWindow::on_playButton_pressed()
808 {
809   startRepeating(Play_Key);
810 }
811
812 void MainWindow::on_playButton_released()
813 {
814   stopRepeating();
815 }
816
817 void MainWindow::on_pauseButton_pressed()
818 {
819   startRepeating(Pause_Key);
820 }
821
822 void MainWindow::on_pauseButton_released()
823 {
824   stopRepeating();
825 }
826
827 void MainWindow::on_stopButton_pressed()
828 {
829   startRepeating(Stop_Key);
830 }
831
832 void MainWindow::on_stopButton_released()
833 {
834   stopRepeating();
835 }
836
837 void MainWindow::on_ejectButton_pressed()
838 {
839   startRepeating(Eject_Key);
840 }
841
842 void MainWindow::on_ejectButton_released()
843 {
844   stopRepeating();
845 }
846
847
848 // Menu actions:
849
850 void MainWindow::on_actionSelectKeyset_triggered()
851 {
852   selectKeysetForm->show();
853 }
854
855 void MainWindow::on_actionSecondary_Buttons_triggered()
856 {
857   if (!secondaryForm)
858   {
859     secondaryForm = new PIRSecondaryForm(this);
860   }
861
862   secondaryForm->show();
863 }
864
865 void MainWindow::on_actionAbout_triggered()
866 {
867   if (!aboutForm)
868   {
869     aboutForm = new PIRAboutForm(this);
870   }
871
872   aboutForm->show();
873 }
874
875 void MainWindow::on_actionDocumentation_triggered()
876 {
877   if (!documentationForm)
878   {
879     documentationForm = new PIRDocumentationForm(this);
880   }
881
882   documentationForm->show();
883 }
884
885
886 // Other actions:
887
888 void MainWindow::keysetSelectionChanged(
889   QListWidgetItem *item)
890 {
891   if (!item) return;  // Should probably display error message here!
892
893   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
894
895   if (!kwi) return; // Also need to say something here
896   
897   currentKeyset = kwi->getID();
898
899   QSettings settings("pietrzak.org", "Pierogi");
900 //  settings.setValue("currentKeyset", currentKeyset);
901
902   settings.setValue(
903     "currentKeysetMake",
904     makeManager.getMakeString(kwi->getMake()));
905
906   settings.setValue(
907     "currentKeysetName",
908     myKeysets->getDisplayName(currentKeyset));
909
910   enableButtons();
911 }
912
913
914 void MainWindow::finalCleanup()
915 {
916   // Perform any necessary cleanup work here.
917
918   // Make certain that the thread stops repeating:
919   stopRepeating();
920 }
921
922
923 void MainWindow::on_addKeysetButton_clicked()
924 {
925   // Is the current keyset already a favorite?
926   int count = ui->favoriteKeysetsWidget->count();
927   int index = 0;
928   PIRKeysetWidgetItem *kwi = NULL;
929   while (index < count)
930   {
931     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
932       ui->favoriteKeysetsWidget->item(index));
933
934     if (kwi && (kwi->getID() == currentKeyset))
935     {
936       // Current keyset already in list!  No need to continue.
937       return;
938     }
939     ++index;
940   }
941
942   // Ok, add the current keyset to the favorites:
943   PIRMakeName make = myKeysets->getMake(currentKeyset);
944
945   QString name = makeManager.getMakeString(make);
946   name.append(" ");
947   name.append(myKeysets->getDisplayName(currentKeyset));
948
949   ui->favoriteKeysetsWidget->addItem(
950     new PIRKeysetWidgetItem(name, currentKeyset, make));
951
952   // And, add the keyset id to the persistent list:
953   QSettings settings("pietrzak.org", "Pierogi");
954
955   int favSettingsSize = settings.beginReadArray("favorites");
956   settings.endArray();
957
958   settings.beginWriteArray("favorites");
959   settings.setArrayIndex(favSettingsSize);
960 //  settings.setValue("keysetID", currentKeyset);
961
962   settings.setValue(
963     "keysetMake",
964     makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
965
966   settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
967
968   settings.endArray();
969 }
970
971
972 void MainWindow::on_removeKeysetButton_clicked()
973 {
974   // Deleting an item removes it from the list, so just grab the currently
975   // selected item and delete it:
976   QListWidgetItem *item = ui->favoriteKeysetsWidget->currentItem();
977
978   if (item) delete item;
979
980   // Remove this item from the persistent list.  Well, actually, it seems a
981   // little more convenient to just blow away the existing list of favorites
982   // and rewrite it, as modifying an existing QSettings array in the middle
983   // seems a bit hard...
984   QSettings settings("pietrzak.org", "Pierogi");
985
986   settings.remove("favorites");
987
988   int count = ui->favoriteKeysetsWidget->count();
989
990   // If the count is empty, we can stop right here:
991   if (count == 0) return;
992
993   int index = 0;
994   unsigned int id;
995   PIRKeysetWidgetItem *kwi = NULL;
996   settings.beginWriteArray("favorites");
997   while (index < count)
998   {
999     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
1000       ui->favoriteKeysetsWidget->item(index));
1001
1002     settings.setArrayIndex(index);
1003 //    settings.setValue("keysetID", kwi->getID());
1004     id = kwi->getID();
1005
1006     settings.setValue(
1007       "keysetMake",
1008       makeManager.getMakeString(myKeysets->getMake(id)));
1009
1010     settings.setValue("keysetName", myKeysets->getDisplayName(id));
1011
1012     ++index;
1013   }
1014   settings.endArray();
1015 }
1016
1017 void MainWindow::populateFavorites()
1018 {
1019   QSettings settings("pietrzak.org", "Pierogi");
1020
1021   int size = settings.beginReadArray("favorites");
1022   int index = 0;
1023   QString make;
1024   QString name;
1025   PIRKeysetWidgetItem *kwi;
1026
1027   while (index < size)
1028   {
1029     settings.setArrayIndex(index);
1030     make = settings.value("keysetMake").toString();
1031     name = settings.value("keysetName").toString();
1032
1033     kwi = myKeysets->makeKeysetItem(make, name);
1034
1035     // Did the item creation work?
1036     if (kwi)
1037     {
1038       // Keyset does exist, so continue:
1039       ui->favoriteKeysetsWidget->addItem(kwi);
1040     }
1041
1042     ++index;
1043   }
1044
1045   settings.endArray();
1046 }
1047
1048
1049 void MainWindow::startRepeating(
1050   PIRKeyName name)
1051 {
1052   QMutexLocker locker(&commandIFMutex);
1053   if (!commandInFlight)
1054   {
1055     commandInFlight = true;
1056     emit buttonPressed(currentKeyset, name);
1057   }
1058 }
1059
1060
1061 void MainWindow::stopRepeating()
1062 {
1063   QMutexLocker locker(&stopRepeatingMutex);
1064   stopRepeatingFlag = true;
1065 }
1066
1067
1068 QWidget *MainWindow::getSecondaryWindow()
1069 {
1070   return secondaryForm;
1071 }
1072
1073
1074 void MainWindow::selectPrevFavKeyset()
1075 {
1076   int size = ui->favoriteKeysetsWidget->count();
1077
1078   if (size == 0)
1079   {
1080     // No favorites, so nothing to do!
1081     return;
1082   }
1083
1084   int position = ui->favoriteKeysetsWidget->currentRow();
1085
1086   --position;
1087   if (position < 0)
1088   {
1089     position = size - 1;
1090   }
1091
1092   ui->favoriteKeysetsWidget->setCurrentRow(
1093     position,
1094     QItemSelectionModel::ClearAndSelect);
1095
1096   // Tell the user about the change:
1097   QMaemo5InformationBox::information(
1098     0,
1099     ui->favoriteKeysetsWidget->item(position)->text());
1100 }
1101
1102
1103 void MainWindow::selectNextFavKeyset()
1104 {
1105   int size = ui->favoriteKeysetsWidget->count();
1106
1107   if (size == 0)
1108   {
1109     // No favorites, so just return:
1110     return;
1111   }
1112
1113   int position = ui->favoriteKeysetsWidget->currentRow();
1114
1115   ++position;
1116   if (position == size)
1117   {
1118     position = 0;
1119   }
1120
1121   ui->favoriteKeysetsWidget->setCurrentRow(
1122     position,
1123     QItemSelectionModel::ClearAndSelect);
1124
1125   // Tell the user about the change:
1126   QMaemo5InformationBox::information(
1127     0,
1128     ui->favoriteKeysetsWidget->item(position)->text());
1129 }