Fix for volume rocker bug
[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
31 extern PIRMakeMgr makeManager;
32
33
34 MainWindow::MainWindow(QWidget *parent)
35   : QMainWindow(parent),
36     ui(new Ui::MainWindow),
37     selectKeysetForm(0),
38     secondaryForm(0),
39     documentationForm(0),
40     aboutForm(0),
41     currentKeyset(0)
42 {
43   ui->setupUi(this);
44
45   // Make this a Maemo 5 stacked widget:
46   setAttribute(Qt::WA_Maemo5StackedWindow);
47
48   // Collect the keysets:
49   myKeysets = new PIRKeysetManager();
50
51   // Set up the keyset selection window:
52   selectKeysetForm = new PIRSelectKeysetForm(this);
53
54   // Set up the secondary buttons window:
55   secondaryForm = new PIRSecondaryForm(this);
56
57   myKeysets->populateGuiWidget(selectKeysetForm);
58
59   // Remember any favorites the user has already set:
60   populateFavorites();
61
62   QSettings settings("pietrzak.org", "Pierogi");
63   if (settings.contains("currentKeysetName"))
64   {
65     myKeysets->findKeysetID(
66       settings.value("currentKeysetMake").toString(),
67       settings.value("currentKeysetName").toString(),
68       currentKeyset);
69 //    currentKeyset = settings.value("currentKeyset").toInt();
70   }
71
72   enableButtons();
73
74   connect(
75     ui->favoriteKeysetsWidget,
76     SIGNAL(itemActivated(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   if (currentKeyset == kwi->getID())
898   {
899     // We're already on that keyset, so nothing to do:
900     return;
901   }
902   
903   currentKeyset = kwi->getID();
904
905   QSettings settings("pietrzak.org", "Pierogi");
906 //  settings.setValue("currentKeyset", currentKeyset);
907
908   settings.setValue(
909     "currentKeysetMake",
910     makeManager.getMakeString(kwi->getMake()));
911
912   settings.setValue(
913     "currentKeysetName",
914     myKeysets->getDisplayName(currentKeyset));
915
916   enableButtons();
917 }
918
919
920 void MainWindow::finalCleanup()
921 {
922   // Perform any necessary cleanup work here.
923
924   // Make certain that the thread stops repeating:
925   stopRepeating();
926 }
927
928
929 void MainWindow::on_addKeysetButton_clicked()
930 {
931   // Is the current keyset already a favorite?
932   int count = ui->favoriteKeysetsWidget->count();
933   int index = 0;
934   PIRKeysetWidgetItem *kwi = NULL;
935   while (index < count)
936   {
937     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
938       ui->favoriteKeysetsWidget->item(index));
939
940     if (kwi && (kwi->getID() == currentKeyset))
941     {
942       // Current keyset already in list!  No need to continue.
943       return;
944     }
945     ++index;
946   }
947
948   // Ok, add the current keyset to the favorites:
949   PIRMakeName make = myKeysets->getMake(currentKeyset);
950
951   QString name = makeManager.getMakeString(make);
952   name.append(" ");
953   name.append(myKeysets->getDisplayName(currentKeyset));
954
955   ui->favoriteKeysetsWidget->addItem(
956     new PIRKeysetWidgetItem(name, currentKeyset, make));
957
958   // And, add the keyset id to the persistent list:
959   QSettings settings("pietrzak.org", "Pierogi");
960
961   int favSettingsSize = settings.beginReadArray("favorites");
962   settings.endArray();
963
964   settings.beginWriteArray("favorites");
965   settings.setArrayIndex(favSettingsSize);
966 //  settings.setValue("keysetID", currentKeyset);
967
968   settings.setValue(
969     "keysetMake",
970     makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
971
972   settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
973
974   settings.endArray();
975 }
976
977
978 void MainWindow::on_removeKeysetButton_clicked()
979 {
980   // Deleting an item removes it from the list, so just grab the currently
981   // selected item and delete it:
982   QListWidgetItem *item = ui->favoriteKeysetsWidget->currentItem();
983
984   if (item) delete item;
985
986   // Remove this item from the persistent list.  Well, actually, it seems a
987   // little more convenient to just blow away the existing list of favorites
988   // and rewrite it, as modifying an existing QSettings array in the middle
989   // seems a bit hard...
990   QSettings settings("pietrzak.org", "Pierogi");
991
992   settings.remove("favorites");
993
994   int count = ui->favoriteKeysetsWidget->count();
995
996   // If the count is empty, we can stop right here:
997   if (count == 0) return;
998
999   int index = 0;
1000   unsigned int id;
1001   PIRKeysetWidgetItem *kwi = NULL;
1002   settings.beginWriteArray("favorites");
1003   while (index < count)
1004   {
1005     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
1006       ui->favoriteKeysetsWidget->item(index));
1007
1008     settings.setArrayIndex(index);
1009 //    settings.setValue("keysetID", kwi->getID());
1010     id = kwi->getID();
1011
1012     settings.setValue(
1013       "keysetMake",
1014       makeManager.getMakeString(myKeysets->getMake(id)));
1015
1016     settings.setValue("keysetName", myKeysets->getDisplayName(id));
1017
1018     ++index;
1019   }
1020   settings.endArray();
1021 }
1022
1023 void MainWindow::populateFavorites()
1024 {
1025   QSettings settings("pietrzak.org", "Pierogi");
1026
1027   int size = settings.beginReadArray("favorites");
1028   int index = 0;
1029   QString make;
1030   QString name;
1031   PIRKeysetWidgetItem *kwi;
1032
1033   while (index < size)
1034   {
1035     settings.setArrayIndex(index);
1036     make = settings.value("keysetMake").toString();
1037     name = settings.value("keysetName").toString();
1038
1039     kwi = myKeysets->makeKeysetItem(make, name);
1040
1041     // Did the item creation work?
1042     if (kwi)
1043     {
1044       // Keyset does exist, so continue:
1045       ui->favoriteKeysetsWidget->addItem(kwi);
1046     }
1047
1048     ++index;
1049   }
1050
1051   settings.endArray();
1052 }
1053
1054
1055 void MainWindow::startRepeating(
1056   PIRKeyName name)
1057 {
1058   QMutexLocker locker(&commandIFMutex);
1059   if (!commandInFlight)
1060   {
1061     commandInFlight = true;
1062     emit buttonPressed(currentKeyset, name);
1063   }
1064 }
1065
1066
1067 void MainWindow::stopRepeating()
1068 {
1069   QMutexLocker locker(&stopRepeatingMutex);
1070   stopRepeatingFlag = true;
1071 }
1072
1073
1074 QWidget *MainWindow::getSecondaryWindow()
1075 {
1076   return secondaryForm;
1077 }
1078
1079
1080 void MainWindow::selectPrevFavKeyset()
1081 {
1082   int size = ui->favoriteKeysetsWidget->count();
1083
1084   if (size == 0)
1085   {
1086     // No favorites, so nothing to do!
1087     return;
1088   }
1089
1090   int position = ui->favoriteKeysetsWidget->currentRow();
1091
1092   --position;
1093   if (position < 0)
1094   {
1095     position = size - 1;
1096   }
1097
1098   ui->favoriteKeysetsWidget->setCurrentRow(
1099     position,
1100     QItemSelectionModel::ClearAndSelect);
1101
1102   keysetSelectionChanged(ui->favoriteKeysetsWidget->currentItem());
1103
1104   // Tell the user about the change:
1105   QMaemo5InformationBox::information(
1106     0,
1107     ui->favoriteKeysetsWidget->item(position)->text());
1108 }
1109
1110
1111 void MainWindow::selectNextFavKeyset()
1112 {
1113   int size = ui->favoriteKeysetsWidget->count();
1114
1115   if (size == 0)
1116   {
1117     // No favorites, so just return:
1118     return;
1119   }
1120
1121   int position = ui->favoriteKeysetsWidget->currentRow();
1122
1123   ++position;
1124   if (position == size)
1125   {
1126     position = 0;
1127   }
1128
1129   ui->favoriteKeysetsWidget->setCurrentRow(
1130     position,
1131     QItemSelectionModel::ClearAndSelect);
1132
1133   keysetSelectionChanged(ui->favoriteKeysetsWidget->currentItem());
1134
1135   // Tell the user about the change:
1136   QMaemo5InformationBox::information(
1137     0,
1138     ui->favoriteKeysetsWidget->item(position)->text());
1139 }