00001 #include <qdatetime.h>
00002 #include <qlistview.h>
00003 #include <qlayout.h>
00004 #include <qhbox.h>
00005 #include <qlineedit.h>
00006 #include <qpushbutton.h>
00007
00008 #include <qpe/config.h>
00009
00010 #include <opie/odatebookaccess.h>
00011 #include <opie/odatebookaccessbackend_xml.h>
00012
00013 #include "editor.h"
00014 #include "templatemanager.h"
00015
00016 using namespace Datebook;
00017
00018
00019 TemplateManager::TemplateManager() {
00020
00021 }
00022 TemplateManager::~TemplateManager() {
00023 }
00024 bool TemplateManager::save() {
00025 QStringList lst = names();
00026 Config conf( "datebook-templates");
00027 conf.setGroup( "___Names___");
00028 conf.writeEntry( "Names", names(), 0x1f );
00029
00030 for ( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
00031 conf.setGroup( (*it) );
00032 conf.writeEntry( "Uid", value( (*it) ).uid() );
00033 }
00034
00035 return true;
00036 }
00037 bool TemplateManager::load() {
00038 Config conf( "datebook-templates");
00039 conf.setGroup( "___Names___");
00040 QStringList lst = conf.readListEntry( "Names", 0x1f );
00041 for ( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
00042 conf.setGroup( (*it) );
00043 add( (*it), OEvent() );
00044 }
00045 return true;
00046 }
00047
00048 namespace {
00049 class TemplateItem : public QListViewItem {
00050 public:
00051 TemplateItem( QListView*, const QString& text,
00052 const OEvent& );
00053 ~TemplateItem();
00054
00055 QString text()const;
00056 OEvent event()const;
00057
00058 void setText( const QString& text );
00059 void setEvent( const OEvent& );
00060 private:
00061 QString m_text;
00062 OEvent m_event;
00063 };
00064 TemplateItem::TemplateItem( QListView* view,
00065 const QString& text,
00066 const OEvent& ev )
00067 : QListViewItem( view ), m_event(ev) {
00068 QListViewItem::setText( 0, text );
00069 }
00070 TemplateItem::~TemplateItem() {
00071 }
00072 void TemplateItem::setText( const QString& text ) {
00073 QListViewItem::setText( 0, text );
00074 m_text = text;
00075 }
00076 void TemplateItem::setEvent( const OEvent& ev ) {
00077 m_event = ev;
00078 }
00079 QString TemplateItem::text()const {
00080 return m_text;
00081 }
00082 OEvent TemplateItem::event()const {
00083
00084 }
00085
00086 class InputDialog : public QDialog{
00087 public:
00088 InputDialog( const QString& text );
00089 ~InputDialog();
00090
00091 QString text()const;
00092 private:
00093 QLineEdit* m_lneEdit;
00094 };
00095 InputDialog::InputDialog(const QString& text )
00096 : QDialog(0, 0, true ) {
00097 m_lneEdit = new QLineEdit( this );
00098 m_lneEdit->setText( text );
00099 }
00100 InputDialog::~InputDialog() {
00101 }
00102 QString InputDialog::text() const{
00103 return m_lneEdit->text();
00104 }
00105 }
00106
00107 TemplateDialog::TemplateDialog( const TemplateManager& man, Editor* edit )
00108 : QDialog(0, 0, true ) {
00109 QVBoxLayout* lay = new QVBoxLayout(this);
00110 m_view = new QListView( this );
00111 m_view->addColumn( tr("Template Names") );
00112 lay->addWidget( m_view );
00113
00114 QHBox* box = new QHBox( this );
00115 lay->addWidget( box );
00116
00117 QPushButton* b = new QPushButton( box );
00118 b->setText(tr("&Add") );
00119 connect( b, SIGNAL(clicked() ), this, SLOT(slotAdd() ) );
00120
00121 b = new QPushButton( box );
00122 b->setText(tr("&Edit") );
00123 connect( b, SIGNAL(clicked() ), this, SLOT(slotEdit() ) );
00124
00125 b = new QPushButton( box );
00126 b->setText(tr("&Rename") );
00127 connect(b, SIGNAL(clicked() ), this, SLOT(slotRename() ) );
00128
00129 b = new QPushButton( box );
00130 b->setText(tr("Re&move") );
00131 connect(b, SIGNAL(clicked() ), this, SLOT(slotRemove() ) );
00132
00133 init( man );
00134 m_edit = edit;
00135 }
00136 TemplateDialog::~TemplateDialog() {
00137 }
00138 void TemplateDialog::slotAdd() {
00139 if ( m_edit->newEvent( QDate::currentDate() ) ) {
00140 (void)new TemplateItem( m_view, tr("New Template"), m_edit->event() );
00141 }
00142 }
00143 void TemplateDialog::slotEdit() {
00144 TemplateItem* item = static_cast<TemplateItem*>( m_view->currentItem() );
00145 if (!item) return;
00146
00147 if (m_edit->edit( item->event() ) )
00148 item->setEvent( m_edit->event() );
00149
00150 }
00151 void TemplateDialog::slotRemove() {
00152 QListViewItem* item = m_view->currentItem();
00153 if (!item) return;
00154
00155 m_view->takeItem( item );
00156 delete item;
00157 }
00158 void TemplateDialog::slotRename() {
00159 TemplateItem* item = static_cast<TemplateItem*>( m_view->currentItem() );
00160 if (!item) return;
00161
00162 InputDialog dlg( item->text() );
00163 dlg.setCaption( tr("Rename") );
00164 if ( dlg.exec() == QDialog::Accepted )
00165 item->setText( dlg.text() );
00166
00167 }
00168 TemplateManager TemplateDialog::manager()const {
00169 TemplateManager manager;
00170
00171 QListViewItemIterator it(m_view);
00172 while ( it.current() ) {
00173 TemplateItem* item = static_cast<TemplateItem*>( it.current() );
00174 manager.add( item->text(), item->event() );
00175 ++it;
00176 }
00177 return manager;
00178 }
00179 void TemplateDialog::init( const TemplateManager& man ) {
00180 QStringList list = man.names();
00181 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
00182 (void)new TemplateItem( m_view, (*it), man.value( (*it) ) );
00183 }
00184 }