First Extras-Testing Candidate Release
[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
9 #include "pirkeysetwidgetitem.h"
10 #include "pirselectkeysetform.h"
11 #include "pirsecondaryform.h"
12 #include "pirdocumentationform.h"
13 #include "piraboutform.h"
14 #include "pirkeysetmanager.h"
15
16 //#define DEBUGGING
17
18 // Some ugly globals used for thread communications:
19
20 // A global to show that a command is being processed:
21 bool commandInFlight = false;
22 QMutex commandIFMutex;
23
24 // The stopRepeatingFlag boolean is the method used to tell running commands
25 // in the worker thread to stop:
26 bool stopRepeatingFlag = false;
27 QMutex stopRepeatingMutex;
28
29 extern PIRMakeMgr makeManager;
30
31
32 MainWindow::MainWindow(QWidget *parent)
33   : QMainWindow(parent),
34     ui(new Ui::MainWindow),
35     selectKeysetForm(0),
36     secondaryForm(0),
37     documentationForm(0),
38     aboutForm(0),
39     currentKeyset(0)
40 {
41   ui->setupUi(this);
42
43   // Make this a Maemo 5 stacked widget:
44   setAttribute(Qt::WA_Maemo5StackedWindow);
45
46   // Collect the keysets:
47   myKeysets = new PIRKeysetManager(this);
48
49   // Set up the keyset selection window:
50   selectKeysetForm = new PIRSelectKeysetForm(this);
51
52   // Set up the secondary buttons window:
53   secondaryForm = new PIRSecondaryForm(this);
54
55   myKeysets->populateGuiWidget(selectKeysetForm);
56
57   // Remember any favorites the user has already set:
58   populateFavorites();
59
60   QSettings settings("pietrzak.org", "Pierogi");
61   if (settings.contains("currentKeysetName"))
62   {
63     myKeysets->findKeysetID(
64       settings.value("currentKeysetMake").toString(),
65       settings.value("currentKeysetName").toString(),
66       currentKeyset);
67 //    currentKeyset = settings.value("currentKeyset").toInt();
68   }
69
70   enableButtons();
71   secondaryForm->enableButtons(myKeysets, currentKeyset);
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   // This is going to be a little painful...
182   // Main keys
183   emit powerEnabled(myKeysets->hasKey(currentKeyset, Power_Key));
184   emit volumeUpEnabled(myKeysets->hasKey(currentKeyset, VolumeUp_Key));
185   emit volumeDownEnabled(myKeysets->hasKey(currentKeyset, VolumeDown_Key));
186   emit channelUpEnabled(myKeysets->hasKey(currentKeyset, ChannelUp_Key));
187   emit channelDownEnabled(myKeysets->hasKey(currentKeyset, ChannelDown_Key));
188
189   // Main tab labels:
190   emit keysetMakeChanged(
191     makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
192   emit keysetNameChanged(myKeysets->getDisplayName(currentKeyset));
193
194   // Utility keys:
195   emit redEnabled(myKeysets->hasKey(currentKeyset, Red_Key));
196   emit greenEnabled(myKeysets->hasKey(currentKeyset, Green_Key));
197   emit yellowEnabled(myKeysets->hasKey(currentKeyset, Yellow_Key));
198   emit blueEnabled(myKeysets->hasKey(currentKeyset, Blue_Key));
199   emit aspectRatioEnabled(myKeysets->hasKey(currentKeyset, AspectRatio_Key));
200   emit surroundEnabled(myKeysets->hasKey(currentKeyset, Surround_Key));
201   emit audioEnabled(myKeysets->hasKey(currentKeyset, Audio_Key));
202   emit infoEnabled(myKeysets->hasKey(currentKeyset, Info_Key));
203   emit captionsEnabled(myKeysets->hasKey(currentKeyset, Captions_Key));
204   emit sleepEnabled(myKeysets->hasKey(currentKeyset, Sleep_Key));
205   emit inputEnabled(myKeysets->hasKey(currentKeyset, Input_Key));
206   emit muteEnabled(myKeysets->hasKey(currentKeyset, Mute_Key));
207
208   // Keypad keys
209   emit zeroEnabled(myKeysets->hasKey(currentKeyset, Zero_Key));
210   emit oneEnabled(myKeysets->hasKey(currentKeyset, One_Key));
211   emit twoEnabled(myKeysets->hasKey(currentKeyset, Two_Key));
212   emit threeEnabled(myKeysets->hasKey(currentKeyset, Three_Key));
213   emit fourEnabled(myKeysets->hasKey(currentKeyset, Four_Key));
214   emit fiveEnabled(myKeysets->hasKey(currentKeyset, Five_Key));
215   emit sixEnabled(myKeysets->hasKey(currentKeyset, Six_Key));
216   emit sevenEnabled(myKeysets->hasKey(currentKeyset, Seven_Key));
217   emit eightEnabled(myKeysets->hasKey(currentKeyset, Eight_Key));
218   emit nineEnabled(myKeysets->hasKey(currentKeyset, Nine_Key));
219   emit enterEnabled(myKeysets->hasKey(currentKeyset, Enter_Key));
220   emit clearEnabled(myKeysets->hasKey(currentKeyset, Clear_Key));
221   emit dashEnabled(myKeysets->hasKey(currentKeyset, Dash_Key));
222   emit plusOneHundredEnabled(myKeysets->hasKey(currentKeyset, PlusOneHundred_Key));
223   emit doubleDigitEnabled(myKeysets->hasKey(currentKeyset, DoubleDigit_Key));
224   emit prevChannelEnabled(myKeysets->hasKey(currentKeyset, PrevChannel_Key));
225
226   // Menu keys:
227   emit upEnabled(myKeysets->hasKey(currentKeyset, Up_Key));
228   emit downEnabled(myKeysets->hasKey(currentKeyset, Down_Key));
229   emit leftEnabled(myKeysets->hasKey(currentKeyset, Left_Key));
230   emit rightEnabled(myKeysets->hasKey(currentKeyset, Right_Key));
231   emit selectEnabled(myKeysets->hasKey(currentKeyset, Select_Key));
232   emit menuEnabled(myKeysets->hasKey(currentKeyset, Menu_Key));
233   emit exitEnabled(myKeysets->hasKey(currentKeyset, Exit_Key));
234   emit guideEnabled(myKeysets->hasKey(currentKeyset, Guide_Key));
235   emit discMenuEnabled(myKeysets->hasKey(currentKeyset, DiscMenu_Key));
236
237   // Media keys:
238   emit nextEnabled(myKeysets->hasKey(currentKeyset, Next_Key));
239   emit previousEnabled(myKeysets->hasKey(currentKeyset, Previous_Key));
240   emit advanceEnabled(myKeysets->hasKey(currentKeyset, Advance_Key));
241   emit replayEnabled(myKeysets->hasKey(currentKeyset, Replay_Key));
242   emit stepForwardEnabled(myKeysets->hasKey(currentKeyset, StepForward_Key));
243   emit stepBackEnabled(myKeysets->hasKey(currentKeyset, StepBack_Key));
244   emit fastForwardEnabled(myKeysets->hasKey(currentKeyset, FastForward_Key));
245   emit reverseEnabled(myKeysets->hasKey(currentKeyset, Rewind_Key));
246   emit playEnabled(myKeysets->hasKey(currentKeyset, Play_Key));
247   emit pauseEnabled(myKeysets->hasKey(currentKeyset, Pause_Key));
248   emit stopEnabled(myKeysets->hasKey(currentKeyset, Stop_Key));
249   emit ejectEnabled(myKeysets->hasKey(currentKeyset, Eject_Key));
250 }
251
252
253 void MainWindow::receivedExternalWarning(
254   const char *warning)
255 {
256   QMessageBox errBox;
257   errBox.setText(warning);
258   errBox.setIcon(QMessageBox::Warning);
259   errBox.exec();
260 }
261
262
263 // Main tab buttons:
264
265 void MainWindow::on_powerButton_pressed()
266 {
267   startRepeating(Power_Key);
268 }
269
270 void MainWindow::on_powerButton_released()
271 {
272   stopRepeating();
273 }
274
275 void MainWindow::on_mainChannelUpButton_pressed()
276 {
277   startRepeating(ChannelUp_Key);
278 }
279
280 void MainWindow::on_mainChannelUpButton_released()
281 {
282   stopRepeating();
283 }
284
285 void MainWindow::on_mainChannelDownButton_pressed()
286 {
287   startRepeating(ChannelDown_Key);
288 }
289
290 void MainWindow::on_mainChannelDownButton_released()
291 {
292   stopRepeating();
293 }
294
295 void MainWindow::on_mainVolumeUp_pressed()
296 {
297   startRepeating(VolumeUp_Key);
298 }
299
300 void MainWindow::on_mainVolumeUp_released()
301 {
302   stopRepeating();
303 }
304
305 void MainWindow::on_mainVolumeDownButton_pressed()
306 {
307   startRepeating(VolumeDown_Key);
308 }
309
310 void MainWindow::on_mainVolumeDownButton_released()
311 {
312   stopRepeating();
313 }
314
315
316 // Utility tab buttons:
317
318 void MainWindow::on_redButton_pressed()
319 {
320   startRepeating(Red_Key);
321 }
322
323 void MainWindow::on_redButton_released()
324 {
325   stopRepeating();
326 }
327
328 void MainWindow::on_greenButton_pressed()
329 {
330   startRepeating(Green_Key);
331 }
332
333 void MainWindow::on_greenButton_released()
334 {
335   stopRepeating();
336 }
337
338 void MainWindow::on_yellowButton_pressed()
339 {
340   startRepeating(Yellow_Key);
341 }
342
343 void MainWindow::on_yellowButton_released()
344 {
345   stopRepeating();
346 }
347
348 void MainWindow::on_blueButton_pressed()
349 {
350   startRepeating(Blue_Key);
351 }
352
353 void MainWindow::on_blueButton_released()
354 {
355   stopRepeating();
356 }
357
358 void MainWindow::on_aspectRatioButton_pressed()
359 {
360   startRepeating(AspectRatio_Key);
361 }
362
363 void MainWindow::on_aspectRatioButton_released()
364 {
365   stopRepeating();
366 }
367
368 void MainWindow::on_surroundButton_pressed()
369 {
370   startRepeating(Surround_Key);
371 }
372
373 void MainWindow::on_surroundButton_released()
374 {
375   stopRepeating();
376 }
377
378 void MainWindow::on_audioButton_pressed()
379 {
380   startRepeating(Audio_Key);
381 }
382
383 void MainWindow::on_audioButton_released()
384 {
385   stopRepeating();
386 }
387
388 void MainWindow::on_infoButton_pressed()
389 {
390   startRepeating(Info_Key);
391 }
392
393 void MainWindow::on_infoButton_released()
394 {
395   stopRepeating();
396 }
397
398 void MainWindow::on_captionButton_pressed()
399 {
400   startRepeating(Captions_Key);
401 }
402
403 void MainWindow::on_captionButton_released()
404 {
405   stopRepeating();
406 }
407
408 void MainWindow::on_inputButton_pressed()
409 {
410   startRepeating(Input_Key);
411 }
412
413 void MainWindow::on_inputButton_released()
414 {
415   stopRepeating();
416 }
417
418 void MainWindow::on_sleepButton_pressed()
419 {
420   startRepeating(Sleep_Key);
421 }
422
423 void MainWindow::on_sleepButton_released()
424 {
425   stopRepeating();
426 }
427
428 void MainWindow::on_muteButton_pressed()
429 {
430   startRepeating(Mute_Key);
431 }
432
433 void MainWindow::on_muteButton_released()
434 {
435   stopRepeating();
436 }
437
438
439 // Keypad tab buttons:
440
441 void MainWindow::on_oneButton_pressed()
442 {
443   startRepeating(One_Key);
444 }
445
446 void MainWindow::on_oneButton_released()
447 {
448   stopRepeating();
449 }
450
451 void MainWindow::on_twoButton_pressed()
452 {
453   startRepeating(Two_Key);
454 }
455
456 void MainWindow::on_twoButton_released()
457 {
458   stopRepeating();
459 }
460
461 void MainWindow::on_threeButton_pressed()
462 {
463   startRepeating(Three_Key);
464 }
465
466 void MainWindow::on_threeButton_released()
467 {
468   stopRepeating();
469 }
470
471 void MainWindow::on_fourButton_pressed()
472 {
473   startRepeating(Four_Key);
474 }
475
476 void MainWindow::on_fourButton_released()
477 {
478   stopRepeating();
479 }
480
481 void MainWindow::on_fiveButton_pressed()
482 {
483   startRepeating(Five_Key);
484 }
485
486 void MainWindow::on_fiveButton_released()
487 {
488   stopRepeating();
489 }
490
491 void MainWindow::on_sixButton_pressed()
492 {
493   startRepeating(Six_Key);
494 }
495
496 void MainWindow::on_sixButton_released()
497 {
498   stopRepeating();
499 }
500
501 void MainWindow::on_sevenButton_pressed()
502 {
503   startRepeating(Seven_Key);
504 }
505
506 void MainWindow::on_sevenButton_released()
507 {
508   stopRepeating();
509 }
510
511 void MainWindow::on_eightButton_pressed()
512 {
513   startRepeating(Eight_Key);
514 }
515
516 void MainWindow::on_eightButton_released()
517 {
518   stopRepeating();
519 }
520
521 void MainWindow::on_nineButton_pressed()
522 {
523   startRepeating(Nine_Key);
524 }
525
526 void MainWindow::on_nineButton_released()
527 {
528   stopRepeating();
529 }
530
531 void MainWindow::on_zeroButton_pressed()
532 {
533   startRepeating(Zero_Key);
534 }
535
536 void MainWindow::on_zeroButton_released()
537 {
538   stopRepeating();
539 }
540
541 void MainWindow::on_enterButton_pressed()
542 {
543   startRepeating(Enter_Key);
544 }
545
546 void MainWindow::on_enterButton_released()
547 {
548   stopRepeating();
549 }
550
551 void MainWindow::on_clearButton_pressed()
552 {
553   startRepeating(Clear_Key);
554 }
555
556 void MainWindow::on_clearButton_released()
557 {
558   stopRepeating();
559 }
560
561 void MainWindow::on_prevChannelButton_pressed()
562 {
563   startRepeating(PrevChannel_Key);
564 }
565
566 void MainWindow::on_prevChannelButton_released()
567 {
568   stopRepeating();
569 }
570
571 void MainWindow::on_plusOneHundredButton_pressed()
572 {
573   startRepeating(PlusOneHundred_Key);
574 }
575
576 void MainWindow::on_plusOneHundredButton_released()
577 {
578   stopRepeating();
579 }
580
581 void MainWindow::on_dashButton_pressed()
582 {
583   startRepeating(Dash_Key);
584 }
585
586 void MainWindow::on_dashButton_released()
587 {
588   stopRepeating();
589 }
590
591 void MainWindow::on_doubleDigitButton_pressed()
592 {
593   startRepeating(DoubleDigit_Key);
594 }
595
596 void MainWindow::on_doubleDigitButton_released()
597 {
598   stopRepeating();
599 }
600
601
602 // Menu tab buttons:
603
604 void MainWindow::on_upButton_pressed()
605 {
606   startRepeating(Up_Key);
607 }
608
609 void MainWindow::on_upButton_released()
610 {
611   stopRepeating();
612 }
613
614 void MainWindow::on_leftButton_pressed()
615 {
616   startRepeating(Left_Key);
617 }
618
619 void MainWindow::on_leftButton_released()
620 {
621   stopRepeating();
622 }
623
624 void MainWindow::on_rightButton_pressed()
625 {
626   startRepeating(Right_Key);
627 }
628
629 void MainWindow::on_rightButton_released()
630 {
631   stopRepeating();
632 }
633
634 void MainWindow::on_downButton_pressed()
635 {
636   startRepeating(Down_Key);
637 }
638
639 void MainWindow::on_downButton_released()
640 {
641   stopRepeating();
642 }
643
644 void MainWindow::on_selectButton_pressed()
645 {
646   startRepeating(Select_Key);
647 }
648
649 void MainWindow::on_selectButton_released()
650 {
651   stopRepeating();
652 }
653
654 void MainWindow::on_menuButton_pressed()
655 {
656   startRepeating(Menu_Key);
657 }
658
659 void MainWindow::on_menuButton_released()
660 {
661   stopRepeating();
662 }
663
664 void MainWindow::on_exitButton_pressed()
665 {
666   startRepeating(Exit_Key);
667 }
668
669 void MainWindow::on_exitButton_released()
670 {
671   stopRepeating();
672 }
673
674 void MainWindow::on_guideButton_pressed()
675 {
676   startRepeating(Guide_Key);
677 }
678
679 void MainWindow::on_guideButton_released()
680 {
681   stopRepeating();
682 }
683
684 void MainWindow::on_discMenuButton_pressed()
685 {
686   startRepeating(DiscMenu_Key);
687 }
688
689 void MainWindow::on_discMenuButton_released()
690 {
691   stopRepeating();
692 }
693
694
695 // Media tab buttons:
696
697 void MainWindow::on_mediaPreviousButton_pressed()
698 {
699   startRepeating(Previous_Key);
700 }
701
702 void MainWindow::on_mediaPreviousButton_released()
703 {
704   stopRepeating();
705 }
706
707 void MainWindow::on_mediaNextButton_pressed()
708 {
709   startRepeating(Next_Key);
710 }
711
712 void MainWindow::on_mediaNextButton_released()
713 {
714   stopRepeating();
715 }
716
717 void MainWindow::on_replayButton_pressed()
718 {
719   startRepeating(Replay_Key);
720 }
721
722 void MainWindow::on_replayButton_released()
723 {
724   stopRepeating();
725 }
726
727 void MainWindow::on_advanceButton_pressed()
728 {
729   startRepeating(Advance_Key);
730 }
731
732 void MainWindow::on_advanceButton_released()
733 {
734   stopRepeating();
735 }
736
737 void MainWindow::on_stepBackButton_pressed()
738 {
739   startRepeating(StepBack_Key);
740 }
741
742 void MainWindow::on_stepBackButton_released()
743 {
744   stopRepeating();
745 }
746
747 void MainWindow::on_stepForwardButton_pressed()
748 {
749   startRepeating(StepForward_Key);
750 }
751
752 void MainWindow::on_stepForwardButton_released()
753 {
754   stopRepeating();
755 }
756
757 void MainWindow::on_reverseButton_pressed()
758 {
759   startRepeating(Rewind_Key);
760 }
761
762 void MainWindow::on_reverseButton_released()
763 {
764   stopRepeating();
765 }
766
767 void MainWindow::on_fastForwardButton_pressed()
768 {
769   startRepeating(FastForward_Key);
770 }
771
772 void MainWindow::on_fastForwardButton_released()
773 {
774   stopRepeating();
775 }
776
777 void MainWindow::on_playButton_pressed()
778 {
779   startRepeating(Play_Key);
780 }
781
782 void MainWindow::on_playButton_released()
783 {
784   stopRepeating();
785 }
786
787 void MainWindow::on_pauseButton_pressed()
788 {
789   startRepeating(Pause_Key);
790 }
791
792 void MainWindow::on_pauseButton_released()
793 {
794   stopRepeating();
795 }
796
797 void MainWindow::on_stopButton_pressed()
798 {
799   startRepeating(Stop_Key);
800 }
801
802 void MainWindow::on_stopButton_released()
803 {
804   stopRepeating();
805 }
806
807 void MainWindow::on_ejectButton_pressed()
808 {
809   startRepeating(Eject_Key);
810 }
811
812 void MainWindow::on_ejectButton_released()
813 {
814   stopRepeating();
815 }
816
817
818 // Menu actions:
819
820 void MainWindow::on_actionSelectKeyset_triggered()
821 {
822   selectKeysetForm->show();
823 }
824
825 void MainWindow::on_actionSecondary_Buttons_triggered()
826 {
827   if (!secondaryForm)
828   {
829     secondaryForm = new PIRSecondaryForm(this);
830   }
831
832   secondaryForm->show();
833 }
834
835 void MainWindow::on_actionAbout_triggered()
836 {
837   if (!aboutForm)
838   {
839     aboutForm = new PIRAboutForm(this);
840   }
841
842   aboutForm->show();
843 }
844
845 void MainWindow::on_actionDocumentation_triggered()
846 {
847   if (!documentationForm)
848   {
849     documentationForm = new PIRDocumentationForm(this);
850   }
851
852   documentationForm->show();
853 }
854
855
856 // Other actions:
857
858 void MainWindow::keysetSelectionChanged(
859   QListWidgetItem *item)
860 {
861   if (!item) return;  // Should probably say something here!
862
863   PIRKeysetWidgetItem *kwi = dynamic_cast<PIRKeysetWidgetItem *>(item);
864
865   if (!kwi) return; // Also need to say something here
866   
867   currentKeyset = kwi->getID();
868
869   QSettings settings("pietrzak.org", "Pierogi");
870 //  settings.setValue("currentKeyset", currentKeyset);
871
872   settings.setValue(
873     "currentKeysetMake",
874     makeManager.getMakeString(kwi->getMake()));
875
876   settings.setValue(
877     "currentKeysetName",
878     myKeysets->getDisplayName(currentKeyset));
879
880   enableButtons();
881   secondaryForm->enableButtons(myKeysets, currentKeyset);
882 }
883
884
885 void MainWindow::finalCleanup()
886 {
887   // Perform any necessary cleanup work here.
888
889   // Make certain that the thread stops repeating:
890   stopRepeating();
891 }
892
893
894 void MainWindow::on_addKeysetButton_clicked()
895 {
896   // Is the current keyset already a favorite?
897   int count = ui->favoriteKeysetsWidget->count();
898   int index = 0;
899   PIRKeysetWidgetItem *kwi = NULL;
900   while (index < count)
901   {
902     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
903       ui->favoriteKeysetsWidget->item(index));
904
905     if (kwi && (kwi->getID() == currentKeyset))
906     {
907       // Current keyset already in list!  No need to continue.
908       return;
909     }
910     ++index;
911   }
912
913   // Ok, add the current keyset to the favorites:
914   PIRMakeName make = myKeysets->getMake(currentKeyset);
915
916   QString name = makeManager.getMakeString(make);
917   name.append(" ");
918   name.append(myKeysets->getDisplayName(currentKeyset));
919
920   ui->favoriteKeysetsWidget->addItem(
921     new PIRKeysetWidgetItem(name, currentKeyset, make));
922
923   // And, add the keyset id to the persistent list:
924   QSettings settings("pietrzak.org", "Pierogi");
925
926   int favSettingsSize = settings.beginReadArray("favorites");
927   settings.endArray();
928
929   settings.beginWriteArray("favorites");
930   settings.setArrayIndex(favSettingsSize);
931 //  settings.setValue("keysetID", currentKeyset);
932
933   settings.setValue(
934     "keysetMake",
935     makeManager.getMakeString(myKeysets->getMake(currentKeyset)));
936
937   settings.setValue("keysetName", myKeysets->getDisplayName(currentKeyset));
938
939   settings.endArray();
940 }
941
942
943 void MainWindow::on_removeKeysetButton_clicked()
944 {
945   // Deleting an item removes it from the list, so just grab the currently
946   // selected item and delete it:
947   QListWidgetItem *item = ui->favoriteKeysetsWidget->currentItem();
948
949   if (item) delete item;
950
951   // Remove this item from the persistent list.  Well, actually, it seems a
952   // little more convenient to just blow away the existing list of favorites
953   // and rewrite it, as modifying an existing QSettings array in the middle
954   // seems a bit hard...
955   QSettings settings("pietrzak.org", "Pierogi");
956
957   settings.remove("favorites");
958
959   int count = ui->favoriteKeysetsWidget->count();
960
961   // If the count is empty, we can stop right here:
962   if (count == 0) return;
963
964   int index = 0;
965   unsigned int id;
966   PIRKeysetWidgetItem *kwi = NULL;
967   settings.beginWriteArray("favorites");
968   while (index < count)
969   {
970     kwi = dynamic_cast<PIRKeysetWidgetItem *>(
971       ui->favoriteKeysetsWidget->item(index));
972
973     settings.setArrayIndex(index);
974 //    settings.setValue("keysetID", kwi->getID());
975     id = kwi->getID();
976
977     settings.setValue(
978       "keysetMake",
979       makeManager.getMakeString(myKeysets->getMake(id)));
980
981     settings.setValue("keysetName", myKeysets->getDisplayName(id));
982
983     ++index;
984   }
985   settings.endArray();
986 }
987
988 void MainWindow::populateFavorites()
989 {
990   QSettings settings("pietrzak.org", "Pierogi");
991
992   int size = settings.beginReadArray("favorites");
993   int index = 0;
994   QString make;
995   QString name;
996   PIRKeysetWidgetItem *kwi;
997
998   while (index < size)
999   {
1000     settings.setArrayIndex(index);
1001     make = settings.value("keysetMake").toString();
1002     name = settings.value("keysetName").toString();
1003
1004     kwi = myKeysets->makeKeysetItem(make, name);
1005
1006     // Did the item creation work?
1007     if (kwi)
1008     {
1009       // Keyset does exist, so continue:
1010       ui->favoriteKeysetsWidget->addItem(kwi);
1011     }
1012
1013     ++index;
1014   }
1015
1016   settings.endArray();
1017 }
1018
1019
1020 void MainWindow::startRepeating(
1021   PIRKeyName name)
1022 {
1023   QMutexLocker locker(&commandIFMutex);
1024   if (!commandInFlight)
1025   {
1026     commandInFlight = true;
1027     emit buttonPressed(currentKeyset, name);
1028   }
1029 }
1030
1031
1032 void MainWindow::stopRepeating()
1033 {
1034   QMutexLocker locker(&stopRepeatingMutex);
1035   stopRepeatingFlag = true;
1036 }
1037