00001
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
#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
00076
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
00093
00094
00095
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
00114
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();
00120
if( !e.isNull() ) {
00121
QString id = e.attribute(
"id" );
00122
QString app = e.attribute(
"app" );
00123
QString name = e.attribute(
"name");
00124
00125
00126
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 );
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 }