1 /*
2  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
3  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
4  *
5  * Copyright (C) 2015  Ivan Romanov <drizt72@zoho.eu>
6  *
7  * This program 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 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #pragma once
23 
24 #include "abstracttreemodel.h"
25 #include "bencode.h"
26 
27 #include <QString>
28 #include <QList>
29 #include <QPair>
30 
31 class QTextCodec;
32 
33 class BencodeModel : public AbstractTreeModel<Bencode>
34 {
35     Q_OBJECT
36 
37 public:
38     enum class Column
39     {
40         Name,
41         Type,
42         Hex,
43         Value,
44         Count // Trick to count elements in enum
45     };
46 
47     explicit BencodeModel(QObject *parent = 0);
48     ~BencodeModel();
49 
50     void setJson(const QVariant &json);
51     QVariant toJson() const;
52 
53     void setRaw(const QByteArray &raw);
54     QByteArray toRaw() const;
55 
56     bool isValid() const;
57     void resetModified();
58     bool isModified() const;
59 
60     void setTextCodec(QTextCodec *textCodec);
61     QTextCodec *textCodec() const;
62 
63     void setName(const QString &name);
64     QString name() const;
65 
66     void setPrivateTorrent(bool privateTorrent);
67     bool privateTorrent() const;
68 
69     void setUrl(const QString &url);
70     QString url() const;
71 
72     void setPublisher(const QString &publisher);
73     QString publisher() const;
74 
75     void setCreatedBy(const QString &createdBy);
76     QString createdBy() const;
77 
78     void setCreationTime(const QDateTime &creationTime);
79     QDateTime creationTime() const;
80 
81     void setPieceSize(int pieceSize);
82     int pieceSize() const;
83 
84     int pieces() const;
85     QString hash() const;
86     QString magnetLink() const;
87 
88     void setComment(const QString &comment);
89     QString comment() const;
90 
91     void setTrackers(const QStringList &trackers);
92     QStringList trackers() const;
93 
94     void setFiles(const QList<QPair<QString, qlonglong>> &files);
95     QList<QPair<QString, qlonglong>> files() const;
96     qulonglong totalSize() const;
97 
98     void setPieces(const QByteArray &pieces);
99 
100     void up(const QModelIndex &index);
101     void down(const QModelIndex &index);
102     void appendRow(const QModelIndex &parent);
103 
104     void changeType(const QModelIndex &index, Bencode::Type type);
105 
106     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
107 
108     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
109     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
110     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
111 
112     bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
113     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
114     Qt::ItemFlags flags(const QModelIndex &index) const override;
115 
116 private:
117     QString toUnicode(const QByteArray &encoded) const;
118     QByteArray fromUnicode(const QString &unicode) const;
119 
120     // Here saved .torrent file
121     Bencode *_bencode;
122     Bencode *_originBencode;
123 
124     QTextCodec *_textCodec;
125 };
126