File: jd_item.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (167 lines) | stat: -rw-r--r-- 3,357 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * jd_item.cpp - plugin
 * Copyright (C) 2011  Evgeny Khryukin
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <QMimeData>
#include <QIODevice>
#include <QDataStream>
#include "jd_item.h"
//#include <QDebug>

JDItem::JDItem(Type t, const QString& name, const QString& size, const QString& descr, int number, JDItem* parent)
	: parent_(parent)
	, name_(name)
	, size_(size)
	, descr_(descr)
	, number_(number)
	, type_(t)
{
}

JDItem::JDItem(Type t, JDItem* parent)
	: parent_(parent)
	, type_(t)
{
}

JDItem::~JDItem()
{
}

void JDItem::setData(const QString& name, const QString& size, const QString& descr, int number)
{
	name_ = name;
	size_ = size;
	descr_ = descr;
	number_ = number;
}

JDItem* JDItem::parent() const
{
	return parent_;
}

JDItem::Type JDItem::type() const
{
	return type_;
}

QString JDItem::name() const
{
	return name_;
}

QString JDItem::size() const
{
	return size_;
}

QString JDItem::description() const
{
	return descr_;
}

QString JDItem::fullPath() const
{
	QString path;
	if(type_ == File) {
		path = QString("#%1").arg(QString::number(number_));
	}
	if(type_ == Dir) {
		path = name_;
	}
	path = parentPath() + path;

	return path;
}

QString JDItem::parentPath() const
{
	QString path;
	JDItem* it = parent_;
	while(it) {
		path = it->name() + path;
		it = it->parent();
	}
	return path;
}

int JDItem::number() const
{
	return number_;
}

const QString JDItem::mimeType()
{
	return "jditem/file";
}

bool JDItem::operator==(const JDItem& i)
{
	return name_ == i.name() && parent_ == i.parent() && number_ == i.number()
		&& size_ == i.size() && descr_ == i.description();
}

QMimeData* JDItem::mimeData() const
{
	QMimeData* data = new QMimeData();
	QByteArray ba;
	QDataStream out(&ba, QIODevice::WriteOnly);
	out << name_ << size_ << descr_ << number_ << type_;
	out << fullPath(); //Эта информация нам потребуется в моделе
	data->setData(mimeType(), ba);
	return data;
}

void JDItem::fromDataStream(QDataStream *const in)
{
	int t;
	*in >> name_ >> size_ >> descr_ >> number_ >> t;
	type_ = (Type)t;
}


//-----------------------------------
//------ItemsList--------------------
//-----------------------------------

ItemsList::ItemsList() : QList<ProxyItem>()
{
}

//ВНИМАНИЕ! Перед удалением листа желательно явно вызвать метод clear();
ItemsList::~ItemsList()
{
}

bool ItemsList::contains(const JDItem *const item) const
{
	for(int i = 0; i < size(); i++) {
		if(*at(i).item == *item)
			return true;
	}
	return false;
}

void ItemsList::clear()
{
	while(!isEmpty())
		delete this->takeFirst().item;
	QList<ProxyItem>::clear();
}