kitchensync
categoryedit.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <time.h>
00023
00024 #include <qdom.h>
00025 #include <qfile.h>
00026
00027
00028 #include <kconfig.h>
00029
00030 #include "helper.h"
00031
00032 #include "categoryedit.h"
00033
00034
00035 using namespace OpieHelper;
00036
00037 CategoryEdit::CategoryEdit(){
00038 }
00039 CategoryEdit::CategoryEdit(const QString &fileName){
00040 parse( fileName );
00041 }
00042 CategoryEdit::~CategoryEdit(){
00043 }
00044 void CategoryEdit::save(const QString& fileName)const{
00045 QFile file( fileName );
00046 if ( file.open( IO_WriteOnly ) ) {
00047 QTextStream stream( &file );
00048 stream.setEncoding( QTextStream::UnicodeUTF8 );
00049 stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
00050 stream << "<!DOCTYPE CategoryList>" << endl;
00051 stream << "<Categories>" << endl;
00052 for ( QValueList<OpieCategories>::ConstIterator it = m_categories.begin();
00053 it != m_categories.end(); ++it )
00054 {
00055 stream << "<Category id=\""<< escape( (*it).id() ) << "\" ";
00056
00057 if ( !(*it).app().isEmpty() )
00058 stream << " app=\""<< escape( (*it).app() ) << "\" ";
00059
00060 stream << "name=\"" << escape( (*it).name() ) << "\" ";
00061 stream << " />" << endl;
00062 }
00063 stream << "</Categories>" << endl;
00064 file.close();
00065 }
00066 }
00067 int CategoryEdit::addCategory( const QString &name, int id ){
00068 return addCategory( QString::null, name, id );
00069 }
00070 int CategoryEdit::addCategory( const QString &appName, const QString &name, int id ){
00071 if ( id == 0 ) {
00072
00073
00074 id = -1 * (int) ::time(NULL );
00075 while ( ids.contains( id ) ){
00076 id += -1;
00077 if ( id > 0 )
00078 id = -1;
00079 }
00080 }
00081 ids.insert( id, TRUE );
00082 OpieCategories categories(QString::number(id), name, appName);
00083 m_categories.remove( categories);
00084 m_categories.append( categories);
00085 return id;
00086 }
00087
00088
00089
00090
00091
00092
00093 void CategoryEdit::parse( const QString &tempFile ){
00094 clear();
00095
00096 QDomDocument doc( "mydocument" );
00097 QFile f( tempFile );
00098 if ( !f.open( IO_ReadOnly ) )
00099 return;
00100
00101 if ( !doc.setContent( &f ) ) {
00102 f.close();
00103 return;
00104 }
00105 f.close();
00106
00107 QStringList global, contact, organizer;
00108
00109
00110
00111 QDomElement docElem = doc.documentElement();
00112 QDomNode n = docElem.firstChild();
00113 if( docElem.nodeName() == QString::fromLatin1("Categories") ){
00114 while( !n.isNull() ) {
00115 QDomElement e = n.toElement();
00116 if( !e.isNull() ) {
00117 QString id = e.attribute("id" );
00118 QString app = e.attribute("app" );
00119 QString name = e.attribute("name");
00120
00121
00122
00123
00124 if (app == QString::fromLatin1("Calendar") || app == QString::fromLatin1("Todo List") )
00125 organizer.append( name );
00126 else if ( app == QString::fromLatin1("Contacts") )
00127 contact.append( name );
00128 else
00129 global.append( name );
00130
00131 OpieCategories category( id, name, app );
00132 m_categories.append( category );
00133 }
00134 n = n.nextSibling();
00135 }
00136 }
00137 updateKDE( "kaddressbookrc", global + contact );
00138 updateKDE( "korganizerrc", global + organizer );
00139
00140 }
00141 void CategoryEdit::clear()
00142 {
00143 ids.clear();
00144 m_categories.clear();
00145 }
00146 QString CategoryEdit::categoryById( const QString &id, const QString &app )const
00147 {
00148 QValueList<OpieCategories>::ConstIterator it;
00149 QString category;
00150 QString fallback;
00151 for( it = m_categories.begin(); it != m_categories.end(); ++it ){
00152 if( id.stripWhiteSpace() == (*it).id().stripWhiteSpace() ){
00153 if( app == (*it).app() ){
00154 category = (*it).name();
00155 break;
00156 }else{
00157 fallback = (*it).name();
00158 }
00159 }
00160 }
00161 return category.isEmpty() ? fallback : category;
00162 }
00163 QStringList CategoryEdit::categoriesByIds( const QStringList& ids,
00164 const QString& app) {
00165
00166 QStringList list;
00167 QStringList::ConstIterator it;
00168 QString temp;
00169 for ( it = ids.begin(); it != ids.end(); ++it ) {
00170 temp = categoryById( (*it), app );
00171 if (!temp.isEmpty() )
00172 list << temp;
00173 }
00174
00175 return list;
00176 }
00177 void CategoryEdit::updateKDE( const QString& configFile, const QStringList& cats ) {
00178 KConfig conf(configFile);
00179 conf.setGroup("General");
00180 QStringList avail = conf.readListEntry("Custom Categories");
00181 for (QStringList::ConstIterator it = cats.begin(); it != cats.end(); ++it ) {
00182 if (!avail.contains( (*it) ) )
00183 avail << (*it);
00184 }
00185 conf.writeEntry("Custom Categories", avail );
00186 }
|