Show invite menu in wlm chat window immediately
[kdenetwork.git] / filesharing / advanced / nfs / nfsfile.cpp
blob74dfd588229566492be1bb673eca9267c7fa12e5
1 /*
2 Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <pwd.h>
21 #include <time.h>
22 #include <unistd.h>
24 #include <qfileinfo.h>
25 #include <qfile.h>
26 #include <qtextstream.h>
27 #include <qstringlist.h>
28 #include <KStandardDirs>
29 #include <kdebug.h>
30 #include <kmessagebox.h>
31 #include <klocale.h>
32 #include <knfsshare.h>
33 #include <ktemporaryfile.h>
34 #include <kprocess.h>
35 #include <kshell.h>
36 #include "nfsfile.h"
38 NFSFile::NFSFile(const KUrl & url, bool readonly)
40 _lines.setAutoDelete(true);
41 _entries.setAutoDelete(false);
42 _url = url;
43 _readonly = readonly;
46 NFSFile::~NFSFile()
50 void NFSFile::addEntry(NFSEntry *entry)
52 _lines.append(entry);
53 _entries.append(entry);
56 void NFSFile::removeEntry(NFSEntry *entry)
58 _entries.remove(entry);
59 _lines.remove(entry);
62 bool NFSFile::hasEntry(NFSEntry *entry)
64 return 0 < _entries.contains(entry);
68 NFSEntry* NFSFile::getEntryByPath(const QString & path)
70 if( path.isEmpty())
71 return 0L;
73 QString testPath = path.trimmed();
74 if ( testPath[testPath.length()-1] != '/' )
75 testPath += '/';
77 for (NFSEntry* entry = _entries.first(); entry; entry = _entries.next())
79 if (entry->path()==testPath)
80 return entry;
83 return 0L;
86 bool NFSFile::removeEntryByPath(const QString & path) {
87 NFSEntry* entry = getEntryByPath(path);
88 if (!entry)
89 return false;
91 removeEntry(entry);
92 return true;
95 EntryIterator NFSFile::getEntries()
97 return EntryIterator(_entries);
103 bool NFSFile::load()
105 QFile f(_url.path());
107 if ( !f.open(QIODevice::ReadOnly) ) {
108 kError() << "NFSFile::load: Could not open " << _url.path() << endl;
109 return false;
112 _entries.clear();
113 _lines.clear();
115 QTextStream s( &f );
117 bool continuedLine = false; // is true if the line before ended with a backslash
118 QString completeLine;
120 while ( !s.atEnd() )
122 QString currentLine = s.readLine().trimmed();
124 if (continuedLine) {
125 completeLine += currentLine;
126 continuedLine = false;
128 else
129 completeLine = currentLine;
131 // is the line continued in the next line ?
132 if ( completeLine[completeLine.length()-1] == '\\' )
134 continuedLine = true;
135 // remove the ending backslash
136 completeLine.truncate( completeLine.length()-1 );
137 continue;
140 // empty lines
141 if (completeLine.isEmpty()) {
142 _lines.append(new NFSEmptyLine());
143 continue;
146 // comments
147 if ('#' == completeLine[0]) {
148 _lines.append(new NFSComment(completeLine));
149 continue;
152 QString path;
153 QString hosts;
155 // Handle quotation marks
156 if ( completeLine[0] == '"' ) {
157 int i = completeLine.indexOf('"',1);
158 if (i == -1) {
159 kError() << "NFSFile: Parse error: Missing quotation mark: "
160 << completeLine << endl;
161 continue;
163 path = completeLine.mid(1,i-1);
164 hosts = completeLine.mid(i+1);
166 } else { // no quotation marks
167 int i = completeLine.indexOf(' ');
168 if (i == -1)
169 i = completeLine.indexOf('\t');
171 if (i == -1)
172 path = completeLine;
173 else {
174 path = completeLine.left(i);
175 hosts = completeLine.mid(i+1).trimmed();
179 // normalize path
180 if ( path[path.length()-1] != '/' )
181 path += '/';
183 kDebug(5009) << "KNFSShare: Found path: '" << path << "'";
184 NFSEntry *entry = new NFSEntry(path);
185 QStringList hostList = QStringList::split(' ', hosts);
187 if (hostList.isEmpty()) {
188 NFSHost* host = new NFSHost("*");
189 entry->addHost(host);
190 } else {
191 QStringList::iterator it;
192 for ( it = hostList.begin(); it != hostList.end(); ++it ) {
193 NFSHost* host = new NFSHost((*it).trimmed());
194 entry->addHost(host);
195 kDebug(5009) << "KNFSShare: Found host: " << (*it) << " name='"
196 << host->name << "'" << endl;
200 kDebug(5009) << "KNFSShare: Found hosts: " << hosts << "'";
201 this->addEntry(entry);
204 f.close();
207 return true;
211 void NFSFile::saveTo(QTextStream * stream) {
212 Q3PtrListIterator<NFSLine> it(_lines);
214 NFSLine *line;
215 while ( (line = it.current()) != 0 ) {
216 ++it;
217 *stream << line->toString() << endl;
221 bool NFSFile::saveTo(const QString& fileName) {
222 QFile file(fileName);
223 if (!file.open(QIODevice::WriteOnly))
224 return false;
226 QTextStream stream(&file);
227 saveTo(&stream);
228 file.close();
229 return true;
232 bool NFSFile::save()
234 if (QFileInfo(_url.path()).isWritable() ) {
235 saveTo(_url.path());
236 } else
239 KTemporaryFile tempFile;
240 tempFile.open();
241 saveTo(tempFile.fileName());
243 QString command = QString("cp %1 %2")
244 .arg(KShell::quoteArg( tempFile.fileName() ),
245 KShell::quoteArg( _url.path() ));
247 if (restartNFSServer)
248 command +=" && exportfs -ra";
250 KProcess proc;
251 if (!QFileInfo(_url.path()).isWritable() || restartNFSServer)
252 proc<<KStandardDirs::findExe("kdesu") << "-d" << "-c"<<command;
253 else
254 proc.setShellCommand(command);
256 if (proc.execute()) {
257 return false;
262 return true;