summaryrefslogtreecommitdiff
path: root/noncore/apps/confedit/listviewitemconffile.cpp
blob: 228421ba14941813372856e3316043fa0c7b4311 (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
/***************************************************************************
 *                                                                         *
 *   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 <qmessagebox.h>
#include <qtextstream.h>
#include <qstring.h>
#include "listviewitemconfigentry.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()
{
  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;
  ListViewItemConfigEntry *item;
  while ( !t.atEnd() )
  {
  	s = t.readLine().stripWhiteSpace();
	//	qDebug( "line: >%s<\n", s.latin1() );
  	if (s.contains("<?xml"))
   	{
    	_valid = false;
			break;
    }else
    if ( s[0] == '[' && s[s.length()-1] == ']' )
   	{
  //    qDebug("got group"+s);
      group = s.mid(1,s.length()-2);
      groupItem = new ListViewItemConfigEntry(this, group );
      insertItem( groupItem );
    } else
    if ( int pos = s.find('=') )
    {
 //     qDebug("got key"+s);
    	item = new ListViewItemConfigEntry(this, group, s );
      groupItem->insertItem( item );
    }
  }	
	confFile.close();
 	unchanged();
	setExpandable( _valid );
}


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

void ListViewItemConfFile::revert()
{
	if (_changed)
 	{
    parseFile();
  }else{
	 	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();
  }
}

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()+"~";
}