File: fr_unpack.cpp

package info (click to toggle)
fatrat-unpack 1.1.2-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 1,104 kB
  • ctags: 2,579
  • sloc: cpp: 18,714; makefile: 6
file content (180 lines) | stat: -rw-r--r-- 4,446 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
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
FatRat download manager
http://fatrat.dolezel.info

Copyright (C) 2006-2008 Lubos Dolezel <lubos a dolezel.info>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.

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 program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <fatrat/fatrat.h>
#include <fatrat/Transfer.h>

#include <QDir>
#include <QTranslator>
#include <QLocale>
#include <QFileInfo>
#include <QMessageBox>

#include "config.h"
#include "RarUnpacker.h"
#include "FilesDlg.h"
#ifdef WITH_ZIP
#	include "ZipUnpacker.h"
#endif

UnpackerEntry g_archivators[] = {
		{ ".rar", RarUnpacker::create, RarUnpacker::supported },
#ifdef WITH_ZIP
		{ ".zip", ZipUnpacker::create, ZipUnpacker::supported },
#endif
};

struct ArchiveEntry
{
	QString path;
	UnpackerEntry* unpacker;
};

void unpackArchive(Transfer* t, Queue* q);
static void searchDirectory(QString absolute, QString relative, QList<ArchiveEntry>& out);
static UnpackerEntry* isArchive(QString name);

__attribute__ ((constructor)) void init()
{
	//Q_INIT_RESOURCE(unpack);
	
#ifdef WITH_NLS
	static QTranslator translator;
	{
		QString fname = QString("fatrat-unpack_") + QLocale::system().name();
		translator.load(fname, getDataFileDir("/lang", fname));
		QCoreApplication::installTranslator(&translator);
	}
#endif
	
	MenuAction action;
	
	action.strName = QObject::tr("Unpack archive(s)...");
	action.lpfnTriggered = unpackArchive;
	addMenuAction(action);
}

extern "C" __attribute__((visibility("default"))) PluginInfo getInfo()
{
	PluginInfo info;
	
	info.version = "1.1.2";
	info.name = QObject::tr("Unpack plugin");
	info.author = QString::fromUtf8("Luboš Doležel");
	info.website = "http://fatrat.dolezel.info";
	
	return info;
}

void unpackArchive(Transfer* t, Queue* q)
{
	QString transferPath = t->dataPath(true);
	QString dest;
	QFileInfo finfo(transferPath);
	UnpackerEntry* unpacker = 0;
	
	if(!finfo.exists())
		return;
	
	if(!finfo.isDir())
	{
		for(size_t i=0;i<sizeof(g_archivators)/sizeof(g_archivators[0]);i++)
		{
			if(transferPath.endsWith(g_archivators[i].suffix, Qt::CaseInsensitive))
			{
				unpacker = &g_archivators[i];
				break;
			}
		}
		
		if(!unpacker)
		{
			QMessageBox::critical(getMainWindow(), QObject::tr("Unpack an archive"), QObject::tr("This file is not a supported archive!"));
			return;
		}
	}
	else
	{
		QList<ArchiveEntry> eligible;
		searchDirectory(transferPath+'/', "/", eligible);
		
		if(eligible.isEmpty())
		{
			QMessageBox::warning(getMainWindow(), QObject::tr("Unpack an archive"), QObject::tr("No supported archives found."));
			return;
		}
		else if(eligible.size() == 1)
		{
			transferPath = eligible[0].path;
			unpacker = eligible[0].unpacker;
		}
		else
		{
			FilesDlg dlg(getMainWindow());
			QDir dir(transferPath);
			
			for(int i=0;i<eligible.size();i++)
				dlg.listArchives->addItem(dir.relativeFilePath(eligible[i].path));
			
			if(dlg.exec() != QDialog::Accepted || dlg.listArchives->currentRow() < 0)
				return;
			
			int ix = dlg.listArchives->currentRow();
			transferPath = eligible[ix].path;
			unpacker = eligible[ix].unpacker;
		}
	}
	
	unpacker->pCreate(transferPath, t->comment());
}

void searchDirectory(QString absolute, QString relative, QList<ArchiveEntry>& out)
{
	QDir dir(absolute);
	QFileInfoList list = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
	
	foreach(QFileInfo info, list)
	{
		if(info.isDir())
			searchDirectory(absolute+info.fileName()+'/', relative+info.fileName()+'/', out);
		else if(!info.isSymLink())
		{
			UnpackerEntry* ue = isArchive(info.absoluteFilePath());
			if(!ue)
				continue;
			
			ArchiveEntry ae;
			ae.path = info.absoluteFilePath();
			ae.unpacker = ue;
			out << ae;
		}
	}
}

UnpackerEntry* isArchive(QString name)
{
	for(size_t i=0;i<sizeof(g_archivators)/sizeof(g_archivators[0]);i++)
	{
		if(name.endsWith(g_archivators[i].suffix, Qt::CaseInsensitive) && g_archivators[i].pSupported(name))
			return &g_archivators[i];
	}
	return 0;
}