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

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • utils
path-helper.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  utils/path-helper.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2009 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "path-helper.h"
36 
37 #include <kleo/stl_util.h>
38 
39 #include <kleo/exception.h>
40 
41 #include <KDebug>
42 #include <KLocalizedString>
43 
44 #include <QString>
45 #include <QStringList>
46 #include <QFileInfo>
47 #include <QDir>
48 
49 #ifndef Q_MOC_RUN
50 #include <boost/bind.hpp>
51 #endif
52 
53 #include <algorithm>
54 
55 using namespace Kleo;
56 using namespace boost;
57 
58 static QString commonPrefix( const QString & s1, const QString & s2 ) {
59  return QString( s1.data(), std::mismatch( s1.data(), s1.data() + std::min( s1.size(), s2.size() ), s2.data() ).first - s1.data() );
60 }
61 
62 static QString longestCommonPrefix( const QStringList & sl ) {
63  if ( sl.empty() )
64  return QString();
65  QString result = sl.front();
66  Q_FOREACH( const QString & s, sl )
67  result = commonPrefix( s, result );
68  return result;
69 }
70 
71 QString Kleo::heuristicBaseDirectory( const QStringList & fileNames ) {
72  QStringList dirs;
73  Q_FOREACH( const QString & fileName, fileNames )
74  dirs.push_back( QFileInfo( fileName ).path() + QLatin1Char( '/' ) );
75  kDebug() << "dirs" << dirs;
76  const QString candidate = longestCommonPrefix( dirs );
77  const int idx = candidate.lastIndexOf( QLatin1Char( '/' ) );
78  return candidate.left( idx );
79 }
80 
81 QStringList Kleo::makeRelativeTo( const QString & base, const QStringList & fileNames ) {
82 
83  if ( base.isEmpty() )
84  return fileNames;
85  else
86  return makeRelativeTo( QDir( base ), fileNames );
87 
88 }
89 
90 QStringList Kleo::makeRelativeTo( const QDir & baseDir, const QStringList & fileNames ) {
91  return kdtools::transform<QStringList>
92  ( fileNames,
93  boost::bind( &QDir::relativeFilePath, &baseDir, _1 ) );
94 }
95 
96 void Kleo::recursivelyRemovePath( const QString & path ) {
97  const QFileInfo fi( path );
98  if ( fi.isDir() ) {
99  QDir dir( path );
100  Q_FOREACH( const QString & fname, dir.entryList( QDir::AllEntries|QDir::NoDotAndDotDot ) )
101  recursivelyRemovePath( dir.filePath( fname ) );
102  const QString dirName = fi.fileName();
103  dir.cdUp();
104  if ( !dir.rmdir( dirName ) )
105  throw Exception( GPG_ERR_EPERM, i18n("Cannot remove directory %1", path ) );
106  } else {
107  QFile file( path );
108  if ( !file.remove() )
109  throw Exception( GPG_ERR_EPERM, i18n("Cannot remove file %1: %2", path, file.errorString() ) );
110  }
111 }
Kleo::heuristicBaseDirectory
QString heuristicBaseDirectory(const QStringList &files)
Definition: path-helper.cpp:71
QDir::relativeFilePath
QString relativeFilePath(const QString &fileName) const
QList::push_back
void push_back(const T &value)
path-helper.h
QFile::remove
bool remove()
QIODevice::errorString
QString errorString() const
QString::size
int size() const
QDir::filePath
QString filePath(const QString &fileName) const
longestCommonPrefix
static QString longestCommonPrefix(const QStringList &sl)
Definition: path-helper.cpp:62
QFile
QString::lastIndexOf
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
Kleo::makeRelativeTo
QStringList makeRelativeTo(const QDir &dir, const QStringList &files)
Definition: path-helper.cpp:90
QDir::rmdir
bool rmdir(const QString &dirName) const
QList::empty
bool empty() const
commonPrefix
static QString commonPrefix(const QString &s1, const QString &s2)
Definition: path-helper.cpp:58
QFileInfo::isDir
bool isDir() const
QFileInfo::fileName
QString fileName() const
QString::isEmpty
bool isEmpty() const
QList::front
T & front()
QString
Kleo::recursivelyRemovePath
void recursivelyRemovePath(const QString &path)
Definition: path-helper.cpp:96
QDir::cdUp
bool cdUp()
QStringList
QFileInfo
QLatin1Char
QDir
dir
static QString dir(const QString &id)
Definition: filedialog.cpp:54
QDir::entryList
QStringList entryList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
QString::left
QString left(int n) const
QString::data
QChar * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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
  • pimprint

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