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

kleopatra

  • sources
  • kde-4.12
  • 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 #include <boost/bind.hpp>
50 
51 #include <algorithm>
52 
53 using namespace Kleo;
54 using namespace boost;
55 
56 static QString commonPrefix( const QString & s1, const QString & s2 ) {
57  return QString( s1.data(), std::mismatch( s1.data(), s1.data() + std::min( s1.size(), s2.size() ), s2.data() ).first - s1.data() );
58 }
59 
60 static QString longestCommonPrefix( const QStringList & sl ) {
61  if ( sl.empty() )
62  return QString();
63  QString result = sl.front();
64  Q_FOREACH( const QString & s, sl )
65  result = commonPrefix( s, result );
66  return result;
67 }
68 
69 QString Kleo::heuristicBaseDirectory( const QStringList & fileNames ) {
70  QStringList dirs;
71  Q_FOREACH( const QString & fileName, fileNames )
72  dirs.push_back( QFileInfo( fileName ).path() + QLatin1Char( '/' ) );
73  kDebug() << "dirs" << dirs;
74  const QString candidate = longestCommonPrefix( dirs );
75  const int idx = candidate.lastIndexOf( QLatin1Char( '/' ) );
76  return candidate.left( idx );
77 }
78 
79 QStringList Kleo::makeRelativeTo( const QString & base, const QStringList & fileNames ) {
80 
81  if ( base.isEmpty() )
82  return fileNames;
83  else
84  return makeRelativeTo( QDir( base ), fileNames );
85 
86 }
87 
88 QStringList Kleo::makeRelativeTo( const QDir & baseDir, const QStringList & fileNames ) {
89  return kdtools::transform<QStringList>
90  ( fileNames,
91  boost::bind( &QDir::relativeFilePath, &baseDir, _1 ) );
92 }
93 
94 void Kleo::recursivelyRemovePath( const QString & path ) {
95  const QFileInfo fi( path );
96  if ( fi.isDir() ) {
97  QDir dir( path );
98  Q_FOREACH( const QString & fname, dir.entryList( QDir::AllEntries|QDir::NoDotAndDotDot ) )
99  recursivelyRemovePath( dir.filePath( fname ) );
100  const QString dirName = fi.fileName();
101  dir.cdUp();
102  if ( !dir.rmdir( dirName ) )
103  throw Exception( GPG_ERR_EPERM, i18n("Cannot remove directory %1", path ) );
104  } else {
105  QFile file( path );
106  if ( !file.remove() )
107  throw Exception( GPG_ERR_EPERM, i18n("Cannot remove file %1: %2", path, file.errorString() ) );
108  }
109 }
Kleo::heuristicBaseDirectory
QString heuristicBaseDirectory(const QStringList &files)
Definition: path-helper.cpp:69
path-helper.h
longestCommonPrefix
static QString longestCommonPrefix(const QStringList &sl)
Definition: path-helper.cpp:60
Kleo::makeRelativeTo
QStringList makeRelativeTo(const QDir &dir, const QStringList &files)
Definition: path-helper.cpp:88
commonPrefix
static QString commonPrefix(const QString &s1, const QString &s2)
Definition: path-helper.cpp:56
Kleo::recursivelyRemovePath
void recursivelyRemovePath(const QString &path)
Definition: path-helper.cpp:94
dir
static QString dir(const QString &id)
Definition: filedialog.cpp:53
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 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

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