kitchensync

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., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, 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 
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         // code from tt
00073         //generate uid
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  * we parse the simple Category File here
00089  * We also keep track of global Cats
00090  * and Of Organizer and Contact cats and then
00091  * we will add them to the kde side...
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     // print out the element names of all elements that are a direct child
00110     // of the outermost element.
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(); // try to convert the node to an element.
00116         if( !e.isNull() ) { // the node was really an element.
00117         QString id = e.attribute("id" );
00118         QString app = e.attribute("app" );
00119         QString name = e.attribute("name");
00120 
00121                 /*
00122                  * see where it belongs default to global
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 ); // cheater
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys