File: fr_opensubtitles.cpp

package info (click to toggle)
fatrat-opensubtitles 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 176 kB
  • ctags: 47
  • sloc: cpp: 466; makefile: 6
file content (181 lines) | stat: -rw-r--r-- 4,224 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
181
/*
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 <QString>

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

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

#include "config.h"
#include "SubtitlesDlg.h"
#include "SettingsSubtitles.h"

struct MovieFile;
static void searchSubtitles(Transfer* t, Queue* q);
static bool isMovieFile(QString name);
static void searchDirectory(QString absolute, QString relative, QList<MovieFile>& out);

const char* g_movieSuffixes[] = { ".avi", ".mkv", ".mpg", ".mpeg", ".mov", ".divx", 0 };

__attribute__ ((constructor)) void init()
{
	Q_INIT_RESOURCE(opensubtitles);

#ifdef WITH_NLS
	static QTranslator translator;
	{
		QString fname = QString("fatrat-opensubtitles_") + QLocale::system().name();
		translator.load(fname, getDataFileDir("/lang", fname));
		QCoreApplication::installTranslator(&translator);
	}
#endif
	
	MenuAction action;
	
	action.strName = QObject::tr("Search for subtitles...");
	action.lpfnTriggered = searchSubtitles;
	addMenuAction(action);
	
	AppTool at;
	
	at.strName = QObject::tr("Subtitles search");
	at.pfnCreate = SubtitlesDlg::create;
	addAppTool(at);
	
	SettingsItem si;
	
	si.icon = QIcon(":/opensubtitles/opensubtitles.png");
	si.title = QObject::tr("Subtitles search");
	si.lpfnCreate = SettingsSubtitles::create;
	
	addSettingsPage(si);
}

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

struct MovieFile
{
	QString path;
	qint64 size;
	
	MovieFile(QString npath, qint64 nsize)
	: path(npath), size(nsize)
	{
	}
	bool operator<(const MovieFile& that) const
	{
		return size > that.size;
	}
};

void searchSubtitles(Transfer* t, Queue* q)
{
	QList<MovieFile> files;
	QString path = t->dataPath(true);
	QFileInfo info(path);
	QString chosenFile;
	
	if(info.isDir())
		searchDirectory(path+'/', "/", files);
	else if(!info.isSymLink())
	{
		if(isMovieFile(path))
			files << MovieFile(QString(), info.size());
	}
	
	if(files.isEmpty())
	{
		QMessageBox::warning(getMainWindow(), "FatRat", QObject::tr("No movie files found."));
		return;
	}
	if(files.size() == 1)
		chosenFile = files[0].path;
	else
	{
		QString filter = "(";
		QString chosenFile;
		
		for(size_t i=0;g_movieSuffixes[i];i++)
		{
			filter += '*';
			filter += g_movieSuffixes[i];
			filter += ' ';
		}
		filter += ')';
		
		chosenFile = QFileDialog::getOpenFileName(getMainWindow(), "FatRat", path, filter);
		
		if(chosenFile.isEmpty())
			return;
	}
	
	SubtitlesDlg* dlg = new SubtitlesDlg(getMainWindow());
	
	dlg->search(chosenFile);
	dlg->show();
}


void searchDirectory(QString absolute, QString relative, QList<MovieFile>& 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())
		{
			if(isMovieFile(info.fileName()))
				out << MovieFile(info.absoluteFilePath(), info.size());
		}
	}
}

bool isMovieFile(QString name)
{
	for(size_t i=0;g_movieSuffixes[i];i++)
	{
		if(name.endsWith(g_movieSuffixes[i], Qt::CaseInsensitive))
			return true;
	}
	return false;
}