summaryrefslogtreecommitdiffstats
path: root/src/common/global/purl.h
blob: 6cbf38b0abb16d2506ea3974d2014807d15b89d2 (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
/***************************************************************************
 *   Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org>             *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/
#ifndef PURL_H
#define PURL_H

#include "common/global/global.h"
#if QT_VERSION<0x040000
#  include <qhttp.h>
#else
#  include <QtNetwork/QHttp>
#  include <QDateTime>
#endif
#include "common/global/log.h"
#include "common/common/purl_base.h"

namespace PURL
{
//----------------------------------------------------------------------------
class Http : public QHttp
{
Q_OBJECT
public:
  Http(const QString &hostname);
  QHttpResponseHeader _header;

private slots:
  void responseHeaderReceivedSlot(const QHttpResponseHeader &rh) { _header = rh; }
};

class Url;
class Directory;
class Private;

enum SeparatorType { UnixSeparator, WindowsSeparator };

//----------------------------------------------------------------------------
class Base
{
public:
  Base(const QString &filepath = QString::null);
  Base(const KURL &url);
  bool operator <(const Base &url) const { return _url<url._url; }
  bool operator ==(const Base &url) const;
  bool operator !=(const Base &url) const { return !(_url==url._url); }
  const KURL &kurl() const { return _url; }
  QString pretty() const;
  bool isEmpty() const { return _url.isEmpty(); }
  bool isLocal() const;
  QString path(SeparatorType type = UnixSeparator) const;             // with ending '/' unless empty path
  QString unterminatedPath(SeparatorType type = UnixSeparator) const; // no ending '/'
  Directory directory() const;
  bool isInto(const Directory &dir) const;
  bool isRelative() const { return _relative; }
  bool exists(QDateTime *lastModified = 0) const;

protected:
  bool _relative;
  KURL _url;
  static Private *_private;

private:
  bool httpUrlExists(bool *ok = 0) const;
};

//----------------------------------------------------------------------------
class Url : public Base
{
public:
  Url() {}
  Url(const KURL &url) : Base(url) {}
  // add correct extension if filename has no extension
  Url(const Directory &path, const QString &filename, FileType type);
  Url(const Directory &path, const QString &filepath);
  static Url fromPathOrUrl(const QString &s);

  Url toFileType(FileType type) const { return toExtension(type.data().extensions[0]); }
  Url toExtension(const QString &extension) const;
  Url appendExtension(const QString &extension) const;

  const FileType::Data &data() const { return fileType().data(); }
  FileType fileType() const;
  QString basename() const; // filename without extension
  QString filename() const; // filename without path
  QString filepath(SeparatorType type = UnixSeparator) const; // filename with path
  QString relativeTo(const Directory &dir, SeparatorType type = UnixSeparator) const;
  Url toAbsolute(const Directory &dir) const;
#if !defined(NO_KDE)
  bool isDosFile() const;
  bool create(Log::Generic &log) const; // do not overwrite
  bool write(const QString &text, Log::Generic &log) const;
  bool copyTo(const Url &destination, Log::Generic &log) const; // do not overwrite
  bool del(Log::Generic &log) const;
#endif

private:
  Url(const QString &filepath) : Base(filepath) {}
};

extern bool findExistingUrl(Url &url); // may transform extension's case if needed

//----------------------------------------------------------------------------
class UrlList : public QValueList<Url>
{
public:
  UrlList() {}
  UrlList(const Url &url) { append(url); }
  UrlList(const QValueList<Url> &list) : QValueList<Url>(list) {}
#if !defined(NO_KDE)
  UrlList(const KURL::List &list);
#endif
};

//----------------------------------------------------------------------------
class Directory : public Base
{
public:
  Directory(const QString &path = QString::null);
  QStringList files(const QString &filter) const;
  Url findMatchingFilename(const QString &filename) const;
  Directory up() const;
  Directory down(const QString &path) const;
  static Directory current();
#if !defined(NO_KDE)
  bool create(Log::Generic &log) const;
#endif
};

} // namespace

#endif