templatemenuaction.cpp
00001 /* 00002 * templatemenuaction.cpp - menu action to select a template 00003 * Program: kalarm 00004 * Copyright © 2005,2008 by David Jarvie <djarvie@kde.org> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kalarm.h" 00022 00023 #include <tdeactionclasses.h> 00024 #include <tdepopupmenu.h> 00025 #include <kdebug.h> 00026 00027 #include "alarmcalendar.h" 00028 #include "alarmevent.h" 00029 #include "functions.h" 00030 #include "templatemenuaction.moc" 00031 00032 00033 TemplateMenuAction::TemplateMenuAction(const TQString& label, const TQString& icon, TQObject* receiver, 00034 const char* slot, TDEActionCollection* actions, const char* name) 00035 : TDEActionMenu(label, icon, actions, name) 00036 { 00037 setDelayed(false); 00038 connect(popupMenu(), TQT_SIGNAL(aboutToShow()), TQT_SLOT(slotInitMenu())); 00039 connect(popupMenu(), TQT_SIGNAL(activated(int)), TQT_SLOT(slotSelected(int))); 00040 connect(this, TQT_SIGNAL(selected(const KAEvent&)), receiver, slot); 00041 } 00042 00043 /****************************************************************************** 00044 * Called when the New From Template action is clicked. 00045 * Creates a popup menu listing all alarm templates, in sorted name order. 00046 */ 00047 void TemplateMenuAction::slotInitMenu() 00048 { 00049 TDEPopupMenu* menu = popupMenu(); 00050 menu->clear(); 00051 mOriginalTexts.clear(); 00052 TQValueList<KAEvent> templates = KAlarm::templateList(); 00053 for (TQValueList<KAEvent>::ConstIterator it = templates.constBegin(); it != templates.constEnd(); ++it) 00054 { 00055 TQString name = (*it).templateName(); 00056 // Insert the template in sorted order 00057 TQStringList::Iterator tit; 00058 for (tit = mOriginalTexts.begin(); 00059 tit != mOriginalTexts.end() && TQString::localeAwareCompare(name, *tit) > 0; 00060 ++tit); 00061 mOriginalTexts.insert(tit, name); 00062 } 00063 for (TQStringList::ConstIterator tit = mOriginalTexts.constBegin(); tit != mOriginalTexts.constEnd(); ++tit) 00064 menu->insertItem(*tit); 00065 } 00066 00067 /****************************************************************************** 00068 * Called when a template is selected from the New From Template popup menu. 00069 * Executes a New Alarm dialog, preset from the selected template. 00070 */ 00071 void TemplateMenuAction::slotSelected(int id) 00072 { 00073 TDEPopupMenu* menu = popupMenu(); 00074 TQString item = mOriginalTexts[menu->indexOf(id)]; 00075 if (!item.isEmpty()) 00076 { 00077 AlarmCalendar* cal = AlarmCalendar::templateCalendarOpen(); 00078 if (cal) 00079 { 00080 KAEvent templ = KAEvent::findTemplateName(*cal, item); 00081 emit selected(templ); 00082 } 00083 } 00084 }