Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

listviewitemconffile.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  ***************************************************************************/
00009 // (c) 2002 Patrick S. Vogt <tille@handhelds.org>
00010 
00011 #include "listviewitemconffile.h"
00012 #include "listviewitemconfigentry.h"
00013 
00014 /* OPIE */
00015 #include <opie2/odebug.h>
00016 using namespace Opie::Core;
00017 
00018 /* QT */
00019 #include <qmessagebox.h>
00020 #include <qtextstream.h>
00021 
00022 #define tr QObject::tr
00023 
00024 ListViewItemConfFile::ListViewItemConfFile(QFileInfo *file, QListView *parent)
00025    : ListViewItemConf(parent), _valid(false)
00026 {
00027   confFileInfo = file;
00028   //  parseFile();
00029   displayText();
00030 }
00031 
00032 ListViewItemConfFile::~ListViewItemConfFile()
00033 {
00034 }
00035 
00036 
00037 void ListViewItemConfFile::displayText()
00038 {
00039   setText(0,(_changed?"*":"")+confFileInfo->fileName());        
00040 }
00041 
00042 QString ListViewItemConfFile::fileName()
00043 {
00044         return confFileInfo->fileName();
00045 }
00046 
00047 void ListViewItemConfFile::parseFile()
00048 {
00049         //odebug << "ListViewItemConfFile::parseFile BEGIN" << oendl; 
00050         QFile confFile(confFileInfo->absFilePath());
00051         if(! confFile.open(IO_ReadOnly))
00052                 QMessageBox::critical(0,tr("Could not open"),tr("The file ")+confFileInfo->fileName()+tr(" could not be opened."),1,0); 
00053         QTextStream t( &confFile );
00054         QString s;
00055   QString group;
00056   ListViewItemConfigEntry *groupItem;
00057   ListViewItemConfigEntry *item;
00058   while ( !t.atEnd() )
00059   {
00060         s = t.readLine().stripWhiteSpace();
00061                 //odebug << "line: >" << s.latin1() << "<\n" << oendl; 
00062         if (s.contains("<?xml"))
00063         {
00064         _valid = false;
00065                         break;
00066     }else
00067     if ( s[0] == '[' && s[s.length()-1] == ']' )
00068         {
00069   //    odebug << "got group"+s << oendl; 
00070       group = s.mid(1,s.length()-2);
00071       if (!groupItem) groupItem = new ListViewItemConfigEntry(this, tr("no group") );
00072       groupItem = new ListViewItemConfigEntry(this, group );
00073       insertItem( groupItem );
00074     } else
00075     if ( int pos = s.find('=') )
00076     {
00077 //      odebug << "got key"+s << oendl; 
00078         if (!groupItem) odebug << "PANIK NO GROUP! >" << group.latin1() << "<" << oendl; 
00079         item = new ListViewItemConfigEntry(this, group, s );
00080       groupItem->insertItem( item );
00081     }
00082   }     
00083         confFile.close();
00084         setExpandable( _valid );
00085 //      odebug << "ListViewItemConfFile::parseFile END" << oendl; 
00086 }
00087 
00088 
00089 void ListViewItemConfFile::remove()
00090 {
00091   QFile::remove(confFileInfo->absFilePath());
00092   QFile::remove(backupFileName());
00093   delete this;
00094 }
00095 
00096 void ListViewItemConfFile::revert()
00097 {
00098         if (!_changed)
00099         {
00100         // read the backup file
00101                 QFile conf(confFileInfo->absFilePath());
00102         QFile back(backupFileName());
00103 
00104           if (!back.open(IO_ReadOnly)) return;
00105         if (!conf.open(IO_WriteOnly)) return;
00106 
00107           #define SIZE 124
00108         char buf[SIZE];
00109           while (int c = back.readBlock(buf, SIZE) ) conf.writeBlock(buf,c);
00110         conf.close();
00111           back.close();
00112   }
00113   parseFile();
00114   expand();
00115 }
00116 
00117 void ListViewItemConfFile::save()
00118 {
00119         if (!_changed) return;
00120         QFile conf(confFileInfo->absFilePath());
00121   QFile back(backupFileName());
00122 
00123   if (!conf.open(IO_ReadOnly)) return;
00124   if (!back.open(IO_WriteOnly)) return;
00125 
00126   char buf[SIZE];
00127   while (int c = conf.readBlock(buf, SIZE) ) back.writeBlock(buf,c);
00128   conf.close();
00129   back.close();
00130 
00131 
00132   if (!conf.open(IO_WriteOnly)) return;
00133         QTextStream *t = new QTextStream( &conf );
00134   for (QListViewItem *it = firstChild(); it!=0;it = it->nextSibling())
00135   {
00136         ((ListViewItemConfigEntry*)it)->save(t);
00137   }
00138         conf.close();
00139         unchanged();
00140 }
00141 
00142 
00143 bool ListViewItemConfFile::revertable()
00144 {
00145         return _changed || QFile(backupFileName()).exists();
00146 }
00147 
00148 QString ListViewItemConfFile::backupFileName()
00149 {
00150         return confFileInfo->absFilePath()+"~";
00151 }
00152 
00153 
00154 void ListViewItemConfFile::expand()
00155 {
00156         QListViewItem *subItem = firstChild();
00157         QListViewItem *toDel;
00158         while(subItem)
00159   {
00160         toDel = subItem;
00161         subItem = subItem->nextSibling();
00162         delete toDel;
00163   }
00164         parseFile();
00165 }

Generated on Sat Nov 5 16:16:48 2005 for OPIE by  doxygen 1.4.2