kitchensync Library API Documentation

categoryedit.cpp

00001 /* 00002 This file is part of KitchenSync. 00003 00004 Copyright (c) 2002,2003 Holger Freyther <freyther@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library 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 GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 00020 */ 00021 00022 #include <time.h> 00023 00024 #include <qdom.h> 00025 #include <qfile.h> 00026 //#include <qstring.h> 00027 00028 #include <kconfig.h> 00029 #include <kdebug.h> 00030 00031 #include "helper.h" 00032 00033 #include "categoryedit.h" 00034 00035 00036 using namespace OpieHelper; 00037 00038 CategoryEdit::CategoryEdit(){ 00039 } 00040 CategoryEdit::CategoryEdit(const QString &fileName){ 00041 parse( fileName ); 00042 } 00043 CategoryEdit::~CategoryEdit(){ 00044 } 00045 void CategoryEdit::save(const QString& fileName)const{ 00046 QFile file( fileName ); 00047 if ( file.open( IO_WriteOnly ) ) { 00048 QTextStream stream( &file ); 00049 stream.setEncoding( QTextStream::UnicodeUTF8 ); 00050 stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl; 00051 stream << "<!DOCTYPE CategoryList>" << endl; 00052 stream << "<Categories>" << endl; 00053 for ( QValueList<OpieCategories>::ConstIterator it = m_categories.begin(); 00054 it != m_categories.end(); ++it ) 00055 { 00056 stream << "<Category id=\""<< escape( (*it).id() ) << "\" "; 00057 00058 if ( !(*it).app().isEmpty() ) 00059 stream << " app=\""<< escape( (*it).app() ) << "\" "; 00060 00061 stream << "name=\"" << escape( (*it).name() ) << "\" "; 00062 stream << " />" << endl; 00063 } 00064 stream << "</Categories>" << endl; 00065 file.close(); 00066 } 00067 } 00068 int CategoryEdit::addCategory( const QString &name, int id ){ 00069 return addCategory( QString::null, name, id ); 00070 } 00071 int CategoryEdit::addCategory( const QString &appName, const QString &name, int id ){ 00072 kdDebug(5226) << "add Category " << appName << " " << name << " " << id << endl; 00073 if ( id == 0 ) { 00074 kdDebug(5226) << "need to generate one " << endl; 00075 // code from tt 00076 //generate uid 00077 id = -1 * (int) ::time(NULL ); 00078 while ( ids.contains( id ) ){ 00079 id += -1; 00080 if ( id > 0 ) 00081 id = -1; 00082 } 00083 } 00084 ids.insert( id, TRUE ); 00085 OpieCategories categories(QString::number(id), name, appName); 00086 m_categories.remove( categories); 00087 m_categories.append( categories); 00088 kdDebug(5226) << "new id is " << id << endl; 00089 return id; 00090 } 00091 /* 00092 * we parse the simple Category File here 00093 * We also keep track of global Cats 00094 * and Of Organizer and Contact cats and then 00095 * we will add them to the kde side... 00096 */ 00097 void CategoryEdit::parse( const QString &tempFile ){ 00098 clear(); 00099 00100 QDomDocument doc( "mydocument" ); 00101 QFile f( tempFile ); 00102 if ( !f.open( IO_ReadOnly ) ) 00103 return; 00104 00105 if ( !doc.setContent( &f ) ) { 00106 f.close(); 00107 return; 00108 } 00109 f.close(); 00110 00111 QStringList global, contact, organizer; 00112 00113 // print out the element names of all elements that are a direct child 00114 // of the outermost element. 00115 QDomElement docElem = doc.documentElement(); 00116 QDomNode n = docElem.firstChild(); 00117 if( docElem.nodeName() == QString::fromLatin1("Categories") ){ 00118 while( !n.isNull() ) { 00119 QDomElement e = n.toElement(); // try to convert the node to an element. 00120 if( !e.isNull() ) { // the node was really an element. 00121 QString id = e.attribute("id" ); 00122 QString app = e.attribute("app" ); 00123 QString name = e.attribute("name"); 00124 00125 /* 00126 * see where it belongs default to global 00127 */ 00128 if (app == QString::fromLatin1("Calendar") || app == QString::fromLatin1("Todo List") ) 00129 organizer.append( name ); 00130 else if ( app == QString::fromLatin1("Contacts") ) 00131 contact.append( name ); 00132 else 00133 global.append( name ); 00134 00135 OpieCategories category( id, name, app ); 00136 m_categories.append( category ); // cheater 00137 } 00138 n = n.nextSibling(); 00139 } 00140 } 00141 updateKDE( "kaddressbookrc", global + contact ); 00142 updateKDE( "korganizerrc", global + organizer ); 00143 00144 } 00145 void CategoryEdit::clear() 00146 { 00147 ids.clear(); 00148 m_categories.clear(); 00149 } 00150 QString CategoryEdit::categoryById( const QString &id, const QString &app )const 00151 { 00152 QValueList<OpieCategories>::ConstIterator it; 00153 QString category; 00154 QString fallback; 00155 for( it = m_categories.begin(); it != m_categories.end(); ++it ){ 00156 if( id.stripWhiteSpace() == (*it).id().stripWhiteSpace() ){ 00157 if( app == (*it).app() ){ 00158 category = (*it).name(); 00159 break; 00160 }else{ 00161 fallback = (*it).name(); 00162 } 00163 } 00164 } 00165 return category.isEmpty() ? fallback : category; 00166 } 00167 QStringList CategoryEdit::categoriesByIds( const QStringList& ids, 00168 const QString& app) { 00169 00170 QStringList list; 00171 QStringList::ConstIterator it; 00172 QString temp; 00173 for ( it = ids.begin(); it != ids.end(); ++it ) { 00174 temp = categoryById( (*it), app ); 00175 if (!temp.isEmpty() ) 00176 list << temp; 00177 } 00178 00179 return list; 00180 } 00181 void CategoryEdit::updateKDE( const QString& configFile, const QStringList& cats ) { 00182 KConfig conf(configFile); 00183 conf.setGroup("General"); 00184 QStringList avail = conf.readListEntry("Custom Categories"); 00185 for (QStringList::ConstIterator it = cats.begin(); it != cats.end(); ++it ) { 00186 if (!avail.contains( (*it) ) ) 00187 avail << (*it); 00188 } 00189 conf.writeEntry("Custom Categories", avail ); 00190 }
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 21 19:46:32 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003