summaryrefslogtreecommitdiff
path: root/noncore/apps/confedit/listviewitemconffile.cpp
blob: a7d6b00c0af34233ab6e1a77467fd65c896b5ee6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
// (c) 2002 Patrick S. Vogt <tille@handhelds.org>

#include "listviewitemconffile.h"
#include "listviewitemconfigentry.h"

/* OPIE */
#include <opie2/odebug.h>
using namespace Opie::Core;

/* QT */
#include <qmessagebox.h>
#include <qtextstream.h>

#define tr QObject::tr

ListViewItemConfFile::ListViewItemConfFile(QFileInfo *file, QListView *parent)
   : ListViewItemConf(parent), _valid(false)
{
  confFileInfo = file;
  //  parseFile();
  displayText();
}

ListViewItemConfFile::~ListViewItemConfFile()
{
}


void ListViewItemConfFile::displayText()
{
  setText(0,(_changed?"*":"")+confFileInfo->fileName());	
}

QString ListViewItemConfFile::fileName()
{
  return confFileInfo->fileName();
}

void ListViewItemConfFile::parseFile()
{
  //odebug << "ListViewItemConfFile::parseFile BEGIN" << oendl;
  QFile confFile(confFileInfo->absFilePath());
  if(! confFile.open(IO_ReadOnly))
    QMessageBox::critical(0,tr("Could not open"),tr("The file ")+confFileInfo->fileName()+tr(" could not be opened."),1,0);

  QTextStream t( &confFile );
  QString s;
  QString group;
  ListViewItemConfigEntry *groupItem = 0;
  ListViewItemConfigEntry *item;
  while ( !t.atEnd() )
  {
    s = t.readLine().stripWhiteSpace();
    //odebug << "line: >" << s.latin1() << "<\n" << oendl;
    if (s.contains("<?xml"))
    {
      _valid = false;
      break;
    }
    else if ( s[0] == '[' && s[s.length()-1] == ']' )
    {
      //odebug << "got group"+s << oendl;
      group = s.mid(1,s.length()-2);
      if (!groupItem)
        groupItem = new ListViewItemConfigEntry(this, tr("no group") );

      groupItem = new ListViewItemConfigEntry(this, group );
      insertItem( groupItem );
    }
    else if ( int pos = s.find('=') )
    {
      //odebug << "got key"+s << oendl;
      if (!groupItem)
        odebug << "PANIC! no group >" << group.latin1() << "<" << oendl;

      item = new ListViewItemConfigEntry(this, group, s );
      groupItem->insertItem( item );
    }
  }	
  confFile.close();
  setExpandable( _valid );
  //odebug << "ListViewItemConfFile::parseFile END" << oendl;
}


void ListViewItemConfFile::remove()
{
  QFile::remove(confFileInfo->absFilePath());
  QFile::remove(backupFileName());
  delete this;
}

void ListViewItemConfFile::revert()
{
	if (!_changed)
 	{
  	// read the backup file
	 	QFile conf(confFileInfo->absFilePath());
  	QFile back(backupFileName());

	  if (!back.open(IO_ReadOnly)) return;
  	if (!conf.open(IO_WriteOnly)) return;

	  #define SIZE 124
  	char buf[SIZE];
	  while (int c = back.readBlock(buf, SIZE) ) conf.writeBlock(buf,c);
  	conf.close();
	  back.close();
  }
  parseFile();
  expand();
}

void ListViewItemConfFile::save()
{
	if (!_changed) return;
 	QFile conf(confFileInfo->absFilePath());
  QFile back(backupFileName());

  if (!conf.open(IO_ReadOnly)) return;
  if (!back.open(IO_WriteOnly)) return;

  char buf[SIZE];
  while (int c = conf.readBlock(buf, SIZE) ) back.writeBlock(buf,c);
  conf.close();
  back.close();


  if (!conf.open(IO_WriteOnly)) return;
 	QTextStream *t = new QTextStream( &conf );
  for (QListViewItem *it = firstChild(); it!=0;it = it->nextSibling())
  {
   	((ListViewItemConfigEntry*)it)->save(t);
  }
	conf.close();
 	unchanged();
}


bool ListViewItemConfFile::revertable()
{
 	return _changed || QFile(backupFileName()).exists();
}

QString ListViewItemConfFile::backupFileName()
{
	return confFileInfo->absFilePath()+"~";
}


void ListViewItemConfFile::expand()
{
	QListViewItem *subItem = firstChild();
	QListViewItem *toDel;
 	while(subItem)
  {
  	toDel = subItem;
   	subItem = subItem->nextSibling();
   	delete toDel;
  }
	parseFile();
}