qBittorrent
rss_item.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
4  * Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
5  * Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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
15  * GNU 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 permission to
22  * link this program with the OpenSSL project's "OpenSSL" library (or with
23  * modified versions of it that use the same license as the "OpenSSL" library),
24  * and distribute the linked executables. You must obey the GNU General Public
25  * License in all respects for all of the code used other than "OpenSSL". If you
26  * modify file(s), you may extend this exception to your version of the file(s),
27  * but you are not obligated to do so. If you do not wish to do so, delete this
28  * exception statement from your version.
29  */
30 
31 #include "rss_item.h"
32 
33 #include <QDebug>
34 #include <QRegularExpression>
35 #include <QStringList>
36 
37 using namespace RSS;
38 
39 const QChar Item::PathSeparator('\\');
40 
41 Item::Item(const QString &path)
42  : m_path(path)
43 {
44 }
45 
47 
48 void Item::setPath(const QString &path)
49 {
50  if (path != m_path)
51  {
52  m_path = path;
53  emit pathChanged(this);
54  }
55 }
56 
57 QString Item::path() const
58 {
59  return m_path;
60 }
61 
62 QString Item::name() const
63 {
64  return relativeName(path());
65 }
66 
67 bool Item::isValidPath(const QString &path)
68 {
69  static const QRegularExpression re(
70  QString(R"(\A[^\%1]+(\%1[^\%1]+)*\z)").arg(Item::PathSeparator)
71  , QRegularExpression::DontCaptureOption);
72 
73  if (path.isEmpty() || !re.match(path).hasMatch())
74  {
75  qDebug() << "Incorrect RSS Item path:" << path;
76  return false;
77  }
78 
79  return true;
80 }
81 
82 QString Item::joinPath(const QString &path1, const QString &path2)
83 {
84  if (path1.isEmpty())
85  return path2;
86 
87  return (path1 + Item::PathSeparator + path2);
88 }
89 
90 QStringList Item::expandPath(const QString &path)
91 {
92  QStringList result;
93  if (path.isEmpty()) return result;
94  // if (!isValidRSSFolderName(folder))
95  // return result;
96 
97  int index = 0;
98  while ((index = path.indexOf(Item::PathSeparator, index)) >= 0)
99  {
100  result << path.left(index);
101  ++index;
102  }
103  result << path;
104 
105  return result;
106 }
107 
108 QString Item::parentPath(const QString &path)
109 {
110  int pos;
111  return ((pos = path.lastIndexOf(Item::PathSeparator)) >= 0 ? path.left(pos) : "");
112 }
113 
114 QString Item::relativeName(const QString &path)
115 {
116  int pos;
117  return ((pos = path.lastIndexOf(Item::PathSeparator)) >= 0 ? path.right(path.size() - (pos + 1)) : path);
118 }
static QString joinPath(const QString &path1, const QString &path2)
Definition: rss_item.cpp:82
~Item() override
Definition: rss_item.cpp:46
void pathChanged(Item *item=nullptr)
static const QChar PathSeparator
Definition: rss_item.h:61
static QStringList expandPath(const QString &path)
Definition: rss_item.cpp:90
static QString parentPath(const QString &path)
Definition: rss_item.cpp:108
static QString relativeName(const QString &path)
Definition: rss_item.cpp:114
QString name() const
Definition: rss_item.cpp:62
Item(const QString &path)
Definition: rss_item.cpp:41
void setPath(const QString &path)
Definition: rss_item.cpp:48
QString m_path
Definition: rss_item.h:86
static bool isValidPath(const QString &path)
Definition: rss_item.cpp:67
QString path() const
Definition: rss_item.cpp:57