• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

knode

  • sources
  • kde-4.12
  • kdepim
  • knode
utilities.cpp
Go to the documentation of this file.
1 /*
2  KNode, the KDE newsreader
3  Copyright (c) 1999-2006 the KNode authors.
4  See file AUTHORS for details
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  You should have received a copy of the GNU General Public License
11  along with this program; if not, write to the Free Software Foundation,
12  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
13 */
14 
15 #include <QByteArray>
16 #include <QListWidget>
17 #include <QRegExp>
18 #include <QCursor>
19 
20 #include <QHBoxLayout>
21 
22 #include <klocale.h>
23 #include <kmessagebox.h>
24 #include <kglobalsettings.h>
25 #include <kdebug.h>
26 #include <kio/netaccess.h>
27 #include <ktemporaryfile.h>
28 #include <kfiledialog.h>
29 #include <kdialog.h>
30 
31 #include "knglobals.h"
32 #include "utilities.h"
33 
34 
35 
36 QString KNSaveHelper::lastPath;
37 
38 KNSaveHelper::KNSaveHelper(QString saveName, QWidget *parent)
39  : p_arent(parent), s_aveName(saveName), file(0), tmpFile(0)
40 {
41 }
42 
43 
44 KNSaveHelper::~KNSaveHelper()
45 {
46  if (file) { // local filesystem, just close the file
47  delete file;
48  } else
49  if (tmpFile) { // network location, initiate transaction
50  tmpFile->close();
51  if (KIO::NetAccess::upload(tmpFile->fileName(),url, 0) == false)
52  KNHelper::displayRemoteFileError();
53  delete tmpFile;
54  }
55 }
56 
57 
58 QFile* KNSaveHelper::getFile(const QString &dialogTitle)
59 {
60  url = KFileDialog::getSaveUrl(QString(lastPath + s_aveName), QString(), p_arent, dialogTitle);
61 
62  if (url.isEmpty())
63  return 0;
64 
65  lastPath = url.upUrl().url();
66 
67  if (url.isLocalFile()) {
68  if (QFileInfo(url.toLocalFile()).exists() &&
69  (KMessageBox::warningContinueCancel(knGlobals.topWidget,
70  i18n("<qt>A file named <b>%1</b> already exists.<br />Do you want to replace it?</qt>", url.path()),
71  dialogTitle, KGuiItem(i18n("&Replace"))) != KMessageBox::Continue)) {
72  return 0;
73  }
74 
75  file = new QFile(url.toLocalFile());
76  if(!file->open(QIODevice::WriteOnly)) {
77  KNHelper::displayExternalFileError();
78  delete file;
79  file = 0;
80  }
81  return file;
82  } else {
83  tmpFile = new KTemporaryFile();
84  if (!tmpFile->open()) {
85  KNHelper::displayTempFileError();
86  delete tmpFile;
87  tmpFile = 0;
88  return 0;
89  }
90  return tmpFile;
91  }
92 }
93 
94 
95 //===============================================================================
96 
97 KUrl KNLoadHelper::l_astPath;
98 
99 KNLoadHelper::KNLoadHelper(QWidget *parent)
100  : p_arent(parent), f_ile(0)
101 {
102 }
103 
104 
105 KNLoadHelper::~KNLoadHelper()
106 {
107  delete f_ile;
108  if (!t_empName.isEmpty())
109  KIO::NetAccess::removeTempFile(t_empName);
110 }
111 
112 
113 QFile* KNLoadHelper::getFile( const QString &dialogTitle )
114 {
115  if (f_ile)
116  return f_ile;
117 
118  KUrl url = KFileDialog::getOpenUrl( l_astPath, QString(), p_arent, dialogTitle );
119 
120  if (url.isEmpty())
121  return 0;
122 
123  l_astPath = url;
124 
125  return setURL(url);
126 }
127 
128 
129 QFile* KNLoadHelper::setURL(const KUrl& url)
130 {
131  if (f_ile)
132  return f_ile;
133 
134  u_rl = url;
135 
136  if (u_rl.isEmpty())
137  return 0;
138 
139  QString fileName;
140  if (!u_rl.isLocalFile()) {
141  if (KIO::NetAccess::download(u_rl, t_empName, 0))
142  fileName = t_empName;
143  } else
144  fileName = u_rl.toLocalFile();
145 
146  if (fileName.isEmpty())
147  return 0;
148 
149  f_ile = new QFile( fileName );
150  if(!f_ile->open(QIODevice::ReadOnly)) {
151  KNHelper::displayExternalFileError();
152  delete f_ile;
153  f_ile = 0;
154  }
155  return f_ile;
156 }
157 
158 
159 //===============================================================================
160 
161 
162 // **** keyboard selection dialog *********************************************
163 int KNHelper::selectDialog(QWidget *parent, const QString &caption, const QStringList &options, int initialValue)
164 {
165  KDialog *dlg = new KDialog( parent );
166  dlg->setCaption( caption );
167  dlg->setButtons( KDialog::Ok|KDialog::Cancel );
168  dlg->setDefaultButton( KDialog::Ok );
169 
170  QFrame *page = new QFrame( dlg );
171  dlg->setMainWidget( page );
172  QHBoxLayout *pageL = new QHBoxLayout(page);
173  pageL->setSpacing(5);
174  pageL->setMargin(8);
175 
176  QListWidget *list = new QListWidget( page );
177  pageL->addWidget(list);
178 
179  QString s;
180  for ( QStringList::ConstIterator it = options.begin(); it != options.end(); ++it ) {
181  s = (*it);
182  // remove accelerators
183  s.replace( QRegExp( "&" ), "" ); // krazy:exclude=doublequote_chars
184  list->addItem( s );
185  }
186 
187  list->setCurrentRow( initialValue );
188  list->setFocus();
189  QObject::connect( list, SIGNAL(itemActivated(QListWidgetItem*)), dlg, SLOT(accept()) );
190  restoreWindowSize("selectBox", dlg, QSize(247,174));
191 
192  int ret;
193  if (dlg->exec())
194  ret = list->currentRow();
195  else
196  ret = -1;
197 
198  saveWindowSize("selectBox", dlg->size());
199  delete dlg;
200  return ret;
201 }
202 
203 // **** window geometry managing *********************************************
204 
205 void KNHelper::saveWindowSize(const QString &name, const QSize &s)
206 {
207  KConfigGroup c(knGlobals.config(), "WINDOW_SIZES");
208  c.writeEntry(name, s);
209 }
210 
211 
212 void KNHelper::restoreWindowSize(const QString &name, QWidget *d, const QSize &defaultSize)
213 {
214  KConfigGroup c(knGlobals.config(), "WINDOW_SIZES");
215 
216  QSize s=c.readEntry(name,defaultSize);
217 
218  if(s.isValid()) {
219  QRect max = KGlobalSettings::desktopGeometry(QCursor::pos());
220  if ( s.width() > max.width() ) s.setWidth( max.width()-5 );
221  if ( s.height() > max.height() ) s.setHeight( max.height()-5 );
222  d->resize(s);
223  }
224 }
225 
226 // **** scramble password strings **********************************************
227 
228 const QString KNHelper::encryptStr(const QString& aStr)
229 {
230  uint i,val,len = aStr.length();
231  QString result;
232 
233  for (i=0; i<len; ++i)
234  {
235  val = aStr[i].toLatin1();
236  val -= ' ';
237  val = (255-' ') - val;
238  result += (char)(val + ' ');
239  }
240 
241  return result;
242 }
243 
244 
245 const QString KNHelper::decryptStr(const QString& aStr)
246 {
247  return encryptStr(aStr);
248 }
249 
250 // **** text rewraping *********************************************************
251 
252 int findBreakPos(const QString &text, int start)
253 {
254  int i;
255  for(i=start;i>=0;i--)
256  if(text[i].isSpace())
257  break;
258  if(i>0)
259  return i;
260  for(i=start;i<(int)text.length();++i) // ok, the line is to long
261  if(text[i].isSpace())
262  break;
263  return i;
264 }
265 
266 
267 void appendTextWPrefix(QString &result, const QString &text, int wrapAt, const QString &prefix)
268 {
269  QString txt=text;
270  int breakPos;
271 
272  while(!txt.isEmpty()) {
273 
274  if((int)(prefix.length()+txt.length()) > wrapAt) {
275  breakPos=findBreakPos(txt,wrapAt-prefix.length());
276  result+=(prefix+txt.left(breakPos)+'\n');
277  txt.remove(0,breakPos+1);
278  } else {
279  result+=(prefix+txt+'\n');
280  txt.clear();
281  }
282  }
283 }
284 
285 
286 QString KNHelper::rewrapStringList(const QStringList &text, int wrapAt, QChar quoteChar, bool stopAtSig, bool alwaysSpace)
287 {
288  QString quoted, lastPrefix, thisPrefix, leftover, thisLine;
289  int breakPos;
290 
291  for(QStringList::ConstIterator line=text.begin(); line!=text.end(); ++line) {
292 
293  if(stopAtSig && (*line)=="-- ")
294  break;
295 
296  thisLine=(*line);
297  if (!alwaysSpace && (thisLine[0]==quoteChar))
298  thisLine.prepend(quoteChar); // second quote level without space
299  else
300  thisLine.prepend( quoteChar + QString( ' ' ) );
301 
302  thisPrefix.clear();
303  QChar c;
304  for(int idx=0; idx<(int)(thisLine.length()); idx++) {
305  c=thisLine.at(idx);
306  if( (c==' ') ||
307  (c==quoteChar) || (c=='>') ||(c=='|') || (c==':') || (c=='#') || (c=='[') || (c=='{'))
308  thisPrefix.append(c);
309  else
310  break;
311  }
312 
313  thisLine.remove(0,thisPrefix.length());
314  thisLine = thisLine.trimmed();
315 
316  if(!leftover.isEmpty()) { // don't break paragraphs, tables and quote levels
317  if(thisLine.isEmpty() || (thisPrefix!=lastPrefix) || thisLine.contains(" ") || thisLine.contains('\t'))
318  appendTextWPrefix(quoted, leftover, wrapAt, lastPrefix);
319  else
320  thisLine.prepend( leftover + ' ' );
321  leftover.clear();
322  }
323 
324  if((int)(thisPrefix.length()+thisLine.length()) > wrapAt) {
325  breakPos=findBreakPos(thisLine,wrapAt-thisPrefix.length());
326  if(breakPos < (int)(thisLine.length())) {
327  leftover=thisLine.right(thisLine.length()-breakPos-1);
328  thisLine.truncate(breakPos);
329  }
330  }
331 
332  quoted+=thisPrefix+thisLine+'\n';
333  lastPrefix=thisPrefix;
334  }
335 
336  if (!leftover.isEmpty())
337  appendTextWPrefix(quoted, leftover, wrapAt, lastPrefix);
338 
339  return quoted;
340 }
341 
342 // **** misc. message-boxes **********************************************************
343 
344 void KNHelper::displayInternalFileError(QWidget *w)
345 {
346  KMessageBox::error((w!=0)? w : knGlobals.topWidget, i18n("Unable to load/save configuration.\nWrong permissions on home folder?\nYou should close KNode now to avoid data loss."));
347 }
348 
349 
350 void KNHelper::displayExternalFileError(QWidget *w)
351 {
352  KMessageBox::error((w!=0)? w : knGlobals.topWidget, i18n("Unable to load/save file."));
353 }
354 
355 
356 void KNHelper::displayRemoteFileError(QWidget *w)
357 {
358  KMessageBox::error((w!=0)? w : knGlobals.topWidget, i18n("Unable to save remote file."));
359 }
360 
361 
362 void KNHelper::displayTempFileError(QWidget *w)
363 {
364  KMessageBox::error((w!=0)? w : knGlobals.topWidget, i18n("Unable to create temporary file."));
365 }
366 
367 
368 
369 int KNHelper::findStringInFile( QFile * file, const char * str )
370 {
371  QByteArray searchBuffer;
372  int currentFilePos, pos;
373 
374  while ( !file->atEnd() ) {
375  currentFilePos = file->pos();
376  searchBuffer = file->read( 4096 );
377  if ( searchBuffer.isEmpty() )
378  return -1;
379 
380  pos = searchBuffer.indexOf( str );
381  if ( pos < 0 ) {
382  if ( !file->atEnd() )
383  file->seek( file->pos() - strlen( str ) );
384  else
385  return -1;
386  } else
387  return currentFilePos + pos;
388  }
389  return -1;
390 }
391 
KNSaveHelper::KNSaveHelper
KNSaveHelper(QString saveName, QWidget *parent)
Definition: utilities.cpp:38
KNSaveHelper::getFile
QFile * getFile(const QString &dialogTitle)
returns a file open for writing
Definition: utilities.cpp:58
KNHelper::findStringInFile
static int findStringInFile(QFile *file, const char *str)
Searches for the string from the current file position.
Definition: utilities.cpp:369
KNLoadHelper::getFile
QFile * getFile() const
returns the file after getFile(QString) of setURL(url) was called
Definition: utilities.h:68
KNHelper::saveWindowSize
static void saveWindowSize(const QString &name, const QSize &s)
Definition: utilities.cpp:205
utilities.h
text
virtual QByteArray text(quint32 serialNumber) const =0
QWidget
QListWidget
KNHelper::restoreWindowSize
static void restoreWindowSize(const QString &name, QWidget *d, const QSize &defaultSize)
Definition: utilities.cpp:212
KDialog
KNHelper::displayRemoteFileError
static void displayRemoteFileError(QWidget *w=0)
use this for remote files
Definition: utilities.cpp:356
KNHelper::encryptStr
static const QString encryptStr(const QString &aStr)
Definition: utilities.cpp:228
KNHelper::displayTempFileError
static void displayTempFileError(QWidget *w=0)
use this for error on temporary files
Definition: utilities.cpp:362
KNHelper::displayExternalFileError
static void displayExternalFileError(QWidget *w=0)
use this for all external files
Definition: utilities.cpp:350
KNHelper::displayInternalFileError
static void displayInternalFileError(QWidget *w=0)
use this for all internal files
Definition: utilities.cpp:344
KNLoadHelper::~KNLoadHelper
~KNLoadHelper()
Definition: utilities.cpp:105
KNHelper::decryptStr
static const QString decryptStr(const QString &aStr)
Definition: utilities.cpp:245
QListWidgetItem
KNHelper::rewrapStringList
static QString rewrapStringList(const QStringList &text, int wrapAt, QChar quoteChar, bool stopAtSig, bool alwaysSpace)
used for rewarping a text when replying to a message or inserting a file into a box ...
Definition: utilities.cpp:286
knglobals.h
findBreakPos
int findBreakPos(const QString &text, int start)
Definition: utilities.cpp:252
KNSaveHelper::~KNSaveHelper
~KNSaveHelper()
Definition: utilities.cpp:44
appendTextWPrefix
void appendTextWPrefix(QString &result, const QString &text, int wrapAt, const QString &prefix)
Definition: utilities.cpp:267
KNHelper::selectDialog
static int selectDialog(QWidget *parent, const QString &caption, const QStringList &options, int initialValue)
list selection dialog, used instead of a popup menu when a select action is called via the keyboard...
Definition: utilities.cpp:163
KNLoadHelper::setURL
QFile * setURL(const KUrl &url)
tries to access the file specified by the url and returns a file open for reading ...
Definition: utilities.cpp:129
KNLoadHelper::KNLoadHelper
KNLoadHelper(QWidget *parent)
Definition: utilities.cpp:99
knGlobals
#define knGlobals
Keep compatibility with the old way.
Definition: knglobals.h:28
QFrame
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:36 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knode

Skip menu "knode"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal