-rw-r--r-- | noncore/tools/clock/clock.cpp | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/noncore/tools/clock/clock.cpp b/noncore/tools/clock/clock.cpp index e681650..0ad69d9 100644 --- a/noncore/tools/clock/clock.cpp +++ b/noncore/tools/clock/clock.cpp | |||
@@ -33,64 +33,66 @@ | |||
33 | #include <qpe/config.h> | 33 | #include <qpe/config.h> |
34 | #include <qpe/timestring.h> | 34 | #include <qpe/timestring.h> |
35 | #include <qpe/alarmserver.h> | 35 | #include <qpe/alarmserver.h> |
36 | #include <qpe/sound.h> | 36 | #include <qpe/sound.h> |
37 | #include <qpe/resource.h> | 37 | #include <qpe/resource.h> |
38 | #include <qsound.h> | 38 | #include <qsound.h> |
39 | #include <qtimer.h> | 39 | #include <qtimer.h> |
40 | 40 | ||
41 | 41 | ||
42 | 42 | ||
43 | #include <qlcdnumber.h> | 43 | #include <qlcdnumber.h> |
44 | #include <qslider.h> | 44 | #include <qslider.h> |
45 | #include <qlabel.h> | 45 | #include <qlabel.h> |
46 | #include <qtimer.h> | 46 | #include <qtimer.h> |
47 | #include <qpushbutton.h> | 47 | #include <qpushbutton.h> |
48 | #include <qtoolbutton.h> | 48 | #include <qtoolbutton.h> |
49 | #include <qpainter.h> | 49 | #include <qpainter.h> |
50 | #include <qmessagebox.h> | 50 | #include <qmessagebox.h> |
51 | #include <qdatetime.h> | 51 | #include <qdatetime.h> |
52 | #include <qspinbox.h> | 52 | #include <qspinbox.h> |
53 | #include <qcombobox.h> | 53 | #include <qcombobox.h> |
54 | #include <qcheckbox.h> | 54 | #include <qcheckbox.h> |
55 | #include <qgroupbox.h> | 55 | #include <qgroupbox.h> |
56 | #include <qlayout.h> | 56 | #include <qlayout.h> |
57 | #include <qhbox.h> | 57 | #include <qhbox.h> |
58 | #include <qlineedit.h> | 58 | #include <qlineedit.h> |
59 | 59 | ||
60 | static const int sw_prec = 2; | 60 | static const int sw_prec = 2; |
61 | static const int magic_daily = 2292922; | 61 | static const int magic_daily = 2292922; |
62 | static const int magic_countdown = 2292923; | 62 | static const int magic_countdown = 2292923; |
63 | static const int magic_snooze = 2292924; | 63 | static const int magic_snooze = 2292924; |
64 | static const int magic_playmp = 2292925; | 64 | static const int magic_playmp = 2292925; |
65 | static const char ALARM_CLOCK_CHANNEL [] = "QPE/Application/clock"; | ||
66 | static const char ALARM_CLOCK_MESSAGE [] = "alarm(QDateTime,int)"; | ||
65 | 67 | ||
66 | #include <math.h> | 68 | #include <math.h> |
67 | #include <unistd.h> | 69 | #include <unistd.h> |
68 | #include <sys/types.h> | 70 | #include <sys/types.h> |
69 | 71 | ||
70 | #include <pthread.h> | 72 | #include <pthread.h> |
71 | 73 | ||
72 | 74 | ||
73 | static void toggleScreenSaver( bool on ) | 75 | static void toggleScreenSaver( bool on ) |
74 | { | 76 | { |
75 | QCopEnvelope e( "QPE/System", "setScreenSaverMode(int)" ); | 77 | QCopEnvelope e( "QPE/System", "setScreenSaverMode(int)" ); |
76 | e << ( on ? QPEApplication::Enable : QPEApplication::DisableSuspend ); | 78 | e << ( on ? QPEApplication::Enable : QPEApplication::DisableSuspend ); |
77 | } | 79 | } |
78 | 80 | ||
79 | static void startPlayer() | 81 | static void startPlayer() |
80 | { | 82 | { |
81 | Config config( "qpe" ); | 83 | Config config( "qpe" ); |
82 | config.setGroup( "Time" ); | 84 | config.setGroup( "Time" ); |
83 | sleep(15); | 85 | sleep(15); |
84 | QCopEnvelope e( "QPE/Application/opieplayer", "setDocument(QString)" ); | 86 | QCopEnvelope e( "QPE/Application/opieplayer", "setDocument(QString)" ); |
85 | e << config.readEntry( "mp3File", "" ); | 87 | e << config.readEntry( "mp3File", "" ); |
86 | } | 88 | } |
87 | 89 | ||
88 | class MySpinBox : public QSpinBox | 90 | class MySpinBox : public QSpinBox |
89 | { | 91 | { |
90 | public: | 92 | public: |
91 | QLineEdit *lineEdit() const { | 93 | QLineEdit *lineEdit() const { |
92 | return editor(); | 94 | return editor(); |
93 | } | 95 | } |
94 | }; | 96 | }; |
95 | 97 | ||
96 | // | 98 | // |
@@ -98,73 +100,73 @@ public: | |||
98 | // | 100 | // |
99 | AlarmDlg::AlarmDlg(QWidget *parent, const char *name, bool modal, | 101 | AlarmDlg::AlarmDlg(QWidget *parent, const char *name, bool modal, |
100 | const QString &txt) : | 102 | const QString &txt) : |
101 | AlarmDlgBase(parent, name, modal) | 103 | AlarmDlgBase(parent, name, modal) |
102 | { | 104 | { |
103 | setCaption( tr("Clock") ); | 105 | setCaption( tr("Clock") ); |
104 | pixmap->setPixmap( Resource::loadPixmap("clock/alarmbell") ); | 106 | pixmap->setPixmap( Resource::loadPixmap("clock/alarmbell") ); |
105 | alarmDlgLabel->setText(txt); | 107 | alarmDlgLabel->setText(txt); |
106 | 108 | ||
107 | connect(snoozeTime, SIGNAL(valueChanged(int)), this, | 109 | connect(snoozeTime, SIGNAL(valueChanged(int)), this, |
108 | SLOT(changePrompt(int))); | 110 | SLOT(changePrompt(int))); |
109 | connect(cmdOk, SIGNAL(clicked()), this, SLOT(checkSnooze())); | 111 | connect(cmdOk, SIGNAL(clicked()), this, SLOT(checkSnooze())); |
110 | } | 112 | } |
111 | 113 | ||
112 | // | 114 | // |
113 | // | 115 | // |
114 | // | 116 | // |
115 | void | 117 | void |
116 | AlarmDlg::setText(const QString &txt) | 118 | AlarmDlg::setText(const QString &txt) |
117 | { | 119 | { |
118 | alarmDlgLabel->setText(txt); | 120 | alarmDlgLabel->setText(txt); |
119 | } | 121 | } |
120 | 122 | ||
121 | // | 123 | // |
122 | // | 124 | // |
123 | // | 125 | // |
124 | void | 126 | void |
125 | AlarmDlg::checkSnooze(void) | 127 | AlarmDlg::checkSnooze(void) |
126 | { | 128 | { |
127 | // | 129 | // |
128 | // Ensure we have only one snooze alarm. | 130 | // Ensure we have only one snooze alarm. |
129 | // | 131 | // |
130 | AlarmServer::deleteAlarm(QDateTime(), "QPE/Application/clock", | 132 | AlarmServer::deleteAlarm(QDateTime(), ALARM_CLOCK_CHANNEL, |
131 | "alarm(QDateTime,int)", magic_snooze); | 133 | ALARM_CLOCK_MESSAGE, magic_snooze); |
132 | 134 | ||
133 | if (snoozeTime->value() > 0) { | 135 | if (snoozeTime->value() > 0) { |
134 | QDateTime wake = QDateTime::currentDateTime(); | 136 | QDateTime wake = QDateTime::currentDateTime(); |
135 | wake = wake.addSecs(snoozeTime->value() * 60); // snoozeTime in minutes | 137 | wake = wake.addSecs(snoozeTime->value() * 60); // snoozeTime in minutes |
136 | 138 | ||
137 | AlarmServer::addAlarm(wake, "QPE/Application/clock", | 139 | AlarmServer::addAlarm(wake, ALARM_CLOCK_CHANNEL, |
138 | "alarm(QDateTime,int)", magic_snooze); | 140 | ALARM_CLOCK_MESSAGE, magic_snooze); |
139 | } | 141 | } |
140 | accept(); | 142 | accept(); |
141 | } | 143 | } |
142 | 144 | ||
143 | 145 | ||
144 | 146 | ||
145 | void | 147 | void |
146 | AlarmDlg::changePrompt(int mins) | 148 | AlarmDlg::changePrompt(int mins) |
147 | { | 149 | { |
148 | cmdOk->setText(mins > 0 ? tr("Snooze") : tr("Close") ); | 150 | cmdOk->setText(mins > 0 ? tr("Snooze") : tr("Close") ); |
149 | } | 151 | } |
150 | 152 | ||
151 | 153 | ||
152 | 154 | ||
153 | Clock::Clock( QWidget * parent, const char *, WFlags f ) | 155 | Clock::Clock( QWidget * parent, const char *, WFlags f ) |
154 | : ClockBase( parent, "clock", f ), swatch_splitms(99), init(FALSE) // No tr | 156 | : ClockBase( parent, "clock", f ), swatch_splitms(99), init(FALSE) // No tr |
155 | { | 157 | { |
156 | alarmDlg = 0; | 158 | alarmDlg = 0; |
157 | swLayout = 0; | 159 | swLayout = 0; |
158 | dayBtn = new QToolButton * [7]; | 160 | dayBtn = new QToolButton * [7]; |
159 | 161 | ||
160 | Config config( "qpe" ); | 162 | Config config( "qpe" ); |
161 | config.setGroup("Time"); | 163 | config.setGroup("Time"); |
162 | ampm = config.readBoolEntry( "AMPM", TRUE ); | 164 | ampm = config.readBoolEntry( "AMPM", TRUE ); |
163 | onMonday = config.readBoolEntry( "MONDAY" ); | 165 | onMonday = config.readBoolEntry( "MONDAY" ); |
164 | 166 | ||
165 | connect( tabs, SIGNAL(currentChanged(QWidget*)), | 167 | connect( tabs, SIGNAL(currentChanged(QWidget*)), |
166 | this, SLOT(tabChanged(QWidget*)) ); | 168 | this, SLOT(tabChanged(QWidget*)) ); |
167 | 169 | ||
168 | analogStopwatch = new AnalogClock( swFrame ); | 170 | analogStopwatch = new AnalogClock( swFrame ); |
169 | stopwatchLcd = new QLCDNumber( swFrame ); | 171 | stopwatchLcd = new QLCDNumber( swFrame ); |
170 | stopwatchLcd->setFrameStyle( QFrame::NoFrame ); | 172 | stopwatchLcd->setFrameStyle( QFrame::NoFrame ); |
@@ -542,65 +544,65 @@ void Clock::tabChanged( QWidget * ) | |||
542 | stopStart->setAccel( Key_Return ); | 544 | stopStart->setAccel( Key_Return ); |
543 | } else if ( tabs->currentPageIndex() == 2 ) { | 545 | } else if ( tabs->currentPageIndex() == 2 ) { |
544 | t->start(1000); | 546 | t->start(1000); |
545 | } | 547 | } |
546 | updateClock(); | 548 | updateClock(); |
547 | } | 549 | } |
548 | 550 | ||
549 | void Clock::setDailyAmPm(int) | 551 | void Clock::setDailyAmPm(int) |
550 | { | 552 | { |
551 | scheduleApplyDailyAlarm(); | 553 | scheduleApplyDailyAlarm(); |
552 | } | 554 | } |
553 | 555 | ||
554 | void Clock::setDailyMinute( int m ) | 556 | void Clock::setDailyMinute( int m ) |
555 | { | 557 | { |
556 | dailyMinute->setPrefix( m <= 9 ? "0" : "" ); | 558 | dailyMinute->setPrefix( m <= 9 ? "0" : "" ); |
557 | } | 559 | } |
558 | 560 | ||
559 | void Clock::dailyEdited() | 561 | void Clock::dailyEdited() |
560 | { | 562 | { |
561 | if ( spinBoxValid(dailyMinute) && spinBoxValid(dailyHour) ) | 563 | if ( spinBoxValid(dailyMinute) && spinBoxValid(dailyHour) ) |
562 | scheduleApplyDailyAlarm(); | 564 | scheduleApplyDailyAlarm(); |
563 | else | 565 | else |
564 | applyAlarmTimer->stop(); | 566 | applyAlarmTimer->stop(); |
565 | } | 567 | } |
566 | 568 | ||
567 | void Clock::enableDaily( bool ) | 569 | void Clock::enableDaily( bool ) |
568 | { | 570 | { |
569 | scheduleApplyDailyAlarm(); | 571 | scheduleApplyDailyAlarm(); |
570 | } | 572 | } |
571 | 573 | ||
572 | void Clock::appMessage( const QCString &msg, const QByteArray &data ) | 574 | void Clock::appMessage( const QCString &msg, const QByteArray &data ) |
573 | { | 575 | { |
574 | if ( msg == "alarm(QDateTime,int)" ) { | 576 | if ( msg == ALARM_CLOCK_MESSAGE ) { |
575 | QDataStream ds(data,IO_ReadOnly); | 577 | QDataStream ds(data,IO_ReadOnly); |
576 | QDateTime when; | 578 | QDateTime when; |
577 | int t; | 579 | int t; |
578 | ds >> when >> t; | 580 | ds >> when >> t; |
579 | QTime theTime( when.time() ); | 581 | QTime theTime( when.time() ); |
580 | if ( t == magic_daily || t == magic_snooze || | 582 | if ( t == magic_daily || t == magic_snooze || |
581 | t == magic_playmp ) { | 583 | t == magic_playmp ) { |
582 | QString msg = tr("<b>Daily Alarm:</b><p>"); | 584 | QString msg = tr("<b>Daily Alarm:</b><p>"); |
583 | QString ts; | 585 | QString ts; |
584 | if ( ampm ) { | 586 | if ( ampm ) { |
585 | bool pm = FALSE; | 587 | bool pm = FALSE; |
586 | int h = theTime.hour(); | 588 | int h = theTime.hour(); |
587 | if (h > 12) { | 589 | if (h > 12) { |
588 | h -= 12; | 590 | h -= 12; |
589 | pm = TRUE; | 591 | pm = TRUE; |
590 | } | 592 | } |
591 | if (h == 0) h = 12; | 593 | if (h == 0) h = 12; |
592 | ts.sprintf( "%02d:%02d %s", h, theTime.minute(), pm?"PM":"AM" ); | 594 | ts.sprintf( "%02d:%02d %s", h, theTime.minute(), pm?"PM":"AM" ); |
593 | } else { | 595 | } else { |
594 | ts.sprintf( "%02d:%02d", theTime.hour(), theTime.minute() ); | 596 | ts.sprintf( "%02d:%02d", theTime.hour(), theTime.minute() ); |
595 | } | 597 | } |
596 | msg += ts; | 598 | msg += ts; |
597 | 599 | ||
598 | if (t == magic_playmp ) { | 600 | if (t == magic_playmp ) { |
599 | pthread_t thread; | 601 | pthread_t thread; |
600 | pthread_create(&thread,NULL, (void * (*) (void *))startPlayer, NULL/* &*/ ); | 602 | pthread_create(&thread,NULL, (void * (*) (void *))startPlayer, NULL/* &*/ ); |
601 | }else { | 603 | }else { |
602 | Sound::soundAlarm(); | 604 | Sound::soundAlarm(); |
603 | alarmCount = 0; | 605 | alarmCount = 0; |
604 | alarmt->start( 5000 ); | 606 | alarmt->start( 5000 ); |
605 | } | 607 | } |
606 | if ( !alarmDlg ) { | 608 | if ( !alarmDlg ) { |
@@ -687,90 +689,93 @@ int Clock::dayBtnIdx( int d ) const | |||
687 | else if ( d == 7 ) | 689 | else if ( d == 7 ) |
688 | return 0; | 690 | return 0; |
689 | else | 691 | else |
690 | return d; | 692 | return d; |
691 | } | 693 | } |
692 | 694 | ||
693 | void Clock::scheduleApplyDailyAlarm() | 695 | void Clock::scheduleApplyDailyAlarm() |
694 | { | 696 | { |
695 | applyAlarmTimer->start( 5000, TRUE ); | 697 | applyAlarmTimer->start( 5000, TRUE ); |
696 | } | 698 | } |
697 | 699 | ||
698 | void Clock::applyDailyAlarm() | 700 | void Clock::applyDailyAlarm() |
699 | { | 701 | { |
700 | if ( !init ) | 702 | if ( !init ) |
701 | return; | 703 | return; |
702 | 704 | ||
703 | applyAlarmTimer->stop(); | 705 | applyAlarmTimer->stop(); |
704 | int minute = dailyMinute->value(); | 706 | int minute = dailyMinute->value(); |
705 | int hour = dailyHour->value(); | 707 | int hour = dailyHour->value(); |
706 | if ( ampm ) { | 708 | if ( ampm ) { |
707 | if (hour == 12) | 709 | if (hour == 12) |
708 | hour = 0; | 710 | hour = 0; |
709 | if (dailyAmPm->currentItem() == 1 ) | 711 | if (dailyAmPm->currentItem() == 1 ) |
710 | hour += 12; | 712 | hour += 12; |
711 | } | 713 | } |
712 | 714 | ||
713 | Config config( "Clock" ); | 715 | Config config( "Clock" ); |
714 | config.setGroup( "Daily Alarm" ); | 716 | config.setGroup( "Daily Alarm" ); |
715 | config.writeEntry( "Hour", hour ); | 717 | config.writeEntry( "Hour", hour ); |
716 | config.writeEntry( "Minute", minute ); | 718 | config.writeEntry( "Minute", minute ); |
717 | 719 | ||
718 | bool enableDaily = dailyEnabled->isChecked(); | 720 | bool enableDaily = dailyEnabled->isChecked(); |
719 | bool wasSound = config.readEntry( "SoundEnabled" ); | ||
720 | bool isSound = sndCheck->isChecked(); | 721 | bool isSound = sndCheck->isChecked(); |
721 | int oldMagic = wasSound ? magic_playmp : magic_daily; | ||
722 | int isMagic = isSound ? magic_playmp : magic_daily; | 722 | int isMagic = isSound ? magic_playmp : magic_daily; |
723 | config.writeEntry( "Enabled", enableDaily ); | 723 | config.writeEntry( "Enabled", enableDaily ); |
724 | config.writeEntry( "SoundEnabled", isSound ); | 724 | config.writeEntry( "SoundEnabled", isSound ); |
725 | 725 | ||
726 | QString exclDays; | 726 | QString exclDays; |
727 | int exclCount = 0; | 727 | int exclCount = 0; |
728 | for ( int i = 1; i <= 7; i++ ) { | 728 | for ( int i = 1; i <= 7; i++ ) { |
729 | if ( !dayBtn[dayBtnIdx(i)]->isOn() ) { | 729 | if ( !dayBtn[dayBtnIdx(i)]->isOn() ) { |
730 | if ( !exclDays.isEmpty() ) | 730 | if ( !exclDays.isEmpty() ) |
731 | exclDays += ","; | 731 | exclDays += ","; |
732 | exclDays += QString::number( i ); | 732 | exclDays += QString::number( i ); |
733 | exclCount++; | 733 | exclCount++; |
734 | } | 734 | } |
735 | } | 735 | } |
736 | config.writeEntry( "ExcludeDays", exclDays ); | 736 | config.writeEntry( "ExcludeDays", exclDays ); |
737 | 737 | ||
738 | /* try to delete both */ | 738 | /* try to delete all */ |
739 | AlarmServer::deleteAlarm(QDateTime(), "QPE/Application/clock", | 739 | AlarmServer::deleteAlarm(QDateTime(), ALARM_CLOCK_CHANNEL, |
740 | "alarm(QDateTime,int)", oldMagic); | 740 | ALARM_CLOCK_MESSAGE, magic_daily); |
741 | AlarmServer::deleteAlarm(QDateTime(), ALARM_CLOCK_CHANNEL, | ||
742 | ALARM_CLOCK_MESSAGE, magic_playmp ); | ||
743 | AlarmServer::deleteAlarm(QDateTime(), ALARM_CLOCK_CHANNEL, | ||
744 | ALARM_CLOCK_MESSAGE, magic_snooze); | ||
745 | |||
741 | if ( enableDaily && exclCount < 7 ) { | 746 | if ( enableDaily && exclCount < 7 ) { |
742 | QDateTime when = nextAlarm( hour, minute ); | 747 | QDateTime when = nextAlarm( hour, minute ); |
743 | AlarmServer::addAlarm(when, "QPE/Application/clock", | 748 | AlarmServer::addAlarm(when, ALARM_CLOCK_CHANNEL, |
744 | "alarm(QDateTime,int)", isMagic); | 749 | ALARM_CLOCK_MESSAGE, isMagic); |
745 | } | 750 | } |
746 | } | 751 | } |
747 | 752 | ||
748 | bool Clock::validDaysSelected(void) | 753 | bool Clock::validDaysSelected(void) |
749 | { | 754 | { |
750 | for ( int i = 1; i <= 7; i++ ) { | 755 | for ( int i = 1; i <= 7; i++ ) { |
751 | if ( dayBtn[dayBtnIdx(i)]->isOn() ) { | 756 | if ( dayBtn[dayBtnIdx(i)]->isOn() ) { |
752 | return TRUE; | 757 | return TRUE; |
753 | } | 758 | } |
754 | } | 759 | } |
755 | return FALSE; | 760 | return FALSE; |
756 | } | 761 | } |
757 | 762 | ||
758 | void Clock::closeEvent( QCloseEvent *e ) | 763 | void Clock::closeEvent( QCloseEvent *e ) |
759 | { | 764 | { |
760 | if (dailyEnabled->isChecked()) { | 765 | if (dailyEnabled->isChecked()) { |
761 | if (!validDaysSelected()) { | 766 | if (!validDaysSelected()) { |
762 | QMessageBox::warning(this, tr("Select Day"), | 767 | QMessageBox::warning(this, tr("Select Day"), |
763 | tr("Daily alarm requires at least\none day to be selected.")); | 768 | tr("Daily alarm requires at least\none day to be selected.")); |
764 | return; | 769 | return; |
765 | } | 770 | } |
766 | } | 771 | } |
767 | 772 | ||
768 | applyDailyAlarm(); | 773 | applyDailyAlarm(); |
769 | ClockBase::closeEvent(e); | 774 | ClockBase::closeEvent(e); |
770 | } | 775 | } |
771 | 776 | ||
772 | bool Clock::spinBoxValid( QSpinBox *sb ) | 777 | bool Clock::spinBoxValid( QSpinBox *sb ) |
773 | { | 778 | { |
774 | bool valid = TRUE; | 779 | bool valid = TRUE; |
775 | QString tv = sb->text(); | 780 | QString tv = sb->text(); |
776 | for ( uint i = 0; i < tv.length(); i++ ) { | 781 | for ( uint i = 0; i < tv.length(); i++ ) { |