00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "categoryedit_p.h"
00022
00023 #include <qpe/categories.h>
00024
00025 #include <qdir.h>
00026 #include <qcheckbox.h>
00027 #include <qlineedit.h>
00028
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031
00032 #include <stdlib.h>
00033
00034
00035 using namespace Qtopia;
00036
00037 class CategoryEditPrivate
00038 {
00039 public:
00040 CategoryEditPrivate( QWidget *parent, const QString &appName )
00041 : mCategories( parent, "" ),
00042 mStrApp( appName )
00043 {
00044 editItem = 0;
00045 mCategories.load( categoryFileName() );
00046 }
00047 Categories mCategories;
00048 QListViewItem *editItem;
00049 QString mStrApp;
00050 QString mVisible;
00051 };
00052
00053 CategoryEdit::CategoryEdit( QWidget *parent, const char *name )
00054 : CategoryEditBase( parent, name )
00055 {
00056 d = 0;
00057 }
00058
00059 CategoryEdit::CategoryEdit( const QArray<int> &recCats,
00060 const QString &appName, const QString &visibleName,
00061 QWidget *parent, const char *name )
00062 : CategoryEditBase( parent, name )
00063 {
00064 d = 0;
00065 setCategories( recCats, appName, visibleName );
00066 }
00067
00068 void CategoryEdit::setCategories( const QArray<int> &recCats,
00069 const QString &appName, const QString &visibleName )
00070 {
00071 if ( !d )
00072 d = new CategoryEditPrivate( (QWidget*)parent(), name() );
00073 d->mStrApp = appName;
00074 d->mVisible = visibleName;
00075
00076 QStringList appCats = d->mCategories.labels( d->mStrApp );
00077 QArray<int> cats = d->mCategories.ids(d->mStrApp, appCats);
00078 lvView->clear();
00079
00080 QStringList::ConstIterator it;
00081 int i, j;
00082 for ( i = 0, it = appCats.begin(); it != appCats.end(); i++, ++it ) {
00083 QCheckListItem *chk;
00084 chk = new QCheckListItem( lvView, (*it), QCheckListItem::CheckBox );
00085 if ( !d->mCategories.isGlobal((*it)) )
00086 chk->setText( 1, tr(d->mVisible) );
00087 else
00088 chk->setText( 1, tr("All") );
00089
00090 for ( j = 0; j < int(recCats.count()); j++ ) {
00091 if ( cats[i] == recCats[j] ) {
00092 chk->setOn( true );
00093 break;
00094 }
00095 }
00096 }
00097 lvView->setSorting( 0, TRUE );
00098 lvView->sort();
00099 if ( lvView->childCount() < 1 )
00100 txtCat->setEnabled( FALSE );
00101 else {
00102 lvView->setSelected( lvView->firstChild(), true );
00103 }
00104 }
00105
00106 CategoryEdit::~CategoryEdit()
00107 {
00108 if ( d )
00109 delete d;
00110 }
00111
00112 void CategoryEdit::slotSetText( QListViewItem *selected )
00113 {
00114 d->editItem = selected;
00115 if ( !d->editItem )
00116 return;
00117 txtCat->setText( d->editItem->text(0) );
00118 txtCat->setEnabled( true );
00119 if ( d->editItem->text(1) == tr("All") )
00120 chkGlobal->setChecked( true );
00121 else
00122 chkGlobal->setChecked( false );
00123 }
00124
00125 void CategoryEdit::slotAdd()
00126 {
00127 QString name = tr( "New Category" );
00128 bool insertOk = FALSE;
00129 int num = 0;
00130 while ( !insertOk ) {
00131 if ( num++ > 0 )
00132 name = tr("New Category ") + QString::number(num);
00133 insertOk = d->mCategories.addCategory( d->mStrApp, name );
00134 }
00135 QCheckListItem *chk;
00136 chk = new QCheckListItem( lvView, name, QCheckListItem::CheckBox );
00137 if ( !chkGlobal->isChecked() )
00138 chk->setText( 1, tr(d->mVisible) );
00139 else
00140 chk->setText( 1, tr("All") );
00141
00142 lvView->setSelected( chk, TRUE );
00143 txtCat->selectAll();
00144 txtCat->setFocus();
00145 }
00146
00147 void CategoryEdit::slotRemove()
00148 {
00149 d->editItem = lvView->selectedItem();
00150 if ( d->editItem ) {
00151 QListViewItem *sibling = d->editItem->nextSibling();
00152
00153 d->mCategories.removeCategory( d->mStrApp, d->editItem->text(0) );
00154
00155 delete d->editItem;
00156 d->editItem = 0;
00157
00158 if ( sibling )
00159 lvView->setSelected( sibling, TRUE );
00160 }
00161 if ( lvView->childCount() < 1 ) {
00162 txtCat->clear();
00163 txtCat->setEnabled( FALSE );
00164 }
00165 }
00166
00167 void CategoryEdit::slotSetGlobal( bool isChecked )
00168 {
00169 if ( d->editItem ) {
00170 if ( isChecked )
00171 d->editItem->setText( 1, tr("All") );
00172 else
00173 d->editItem->setText( 1, tr(d->mVisible) );
00174
00175 d->mCategories.setGlobal( d->mStrApp, d->editItem->text( 0 ), isChecked );
00176 }
00177 }
00178
00179 void CategoryEdit::slotTextChanged( const QString &strNew )
00180 {
00181 if ( d->editItem ) {
00182 if ( chkGlobal->isChecked() )
00183 d->mCategories.renameGlobalCategory( d->editItem->text(0), strNew );
00184 else
00185 d->mCategories.renameCategory( d->mStrApp, d->editItem->text(0), strNew );
00186 d->editItem->setText( 0, strNew );
00187 }
00188 }
00189
00190 QArray<int> CategoryEdit::newCategories()
00191 {
00192 QArray<int> a;
00193 if ( d ) {
00194 d->mCategories.save( categoryFileName() );
00195 QListViewItemIterator it( lvView );
00196 QValueList<int> l;
00197 for ( ; it.current(); ++it ) {
00198 if ( reinterpret_cast<QCheckListItem*>(it.current())->isOn() )
00199 l.append( d->mCategories.id( d->mStrApp, it.current()->text(0) ) );
00200 }
00201 uint i = 0;
00202 a.resize( l.count() );
00203 for ( QValueList<int>::Iterator lit = l.begin(); lit != l.end(); ++lit )
00204 a[i++] = *lit;
00205 }
00206 return a;
00207 }
00208
00209 void CategoryEdit::accept()
00210 {
00211
00212 d->mCategories.save( categoryFileName() );
00213
00214 }
00215
00216 QString categoryFileName()
00217 {
00218 QDir dir = (QString(getenv("HOME")) + "/Settings");
00219 if ( !dir.exists() )
00220 mkdir( dir.path().local8Bit(), 0700 );
00221 return dir.path() + "/" + "Categories" + ".xml";
00222 }
00223
00224 void CategoryEdit::kludge()
00225 {
00226 lvView->setMaximumHeight( 130 );
00227 }