File: screenshotplugin.cpp

package info (click to toggle)
packagesearch 2.10.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,704 kB
  • sloc: cpp: 9,176; perl: 248; makefile: 15; sh: 11
file content (185 lines) | stat: -rw-r--r-- 5,007 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
182
183
184
185
#include <sstream>
#include <iostream>

#include <QAbstractItemView>
#include <qcheckbox.h>
#include <QDebug>
#include <qlayout.h>
#include <qlabel.h>
#include <QImageReader>
#include <QMainWindow>
#include <qmessagebox.h>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <qpoint.h>
#include <QScrollArea>
#include <QVBoxLayout>

#include <helpers.h>
#include <exception.h>

// NPlugin
#include <packagenotfoundexception.h>
#include <iprovider.h>

#include "screenshotplugin.h"

using namespace std;

namespace NPlugin
{

const QString ScreenshotPlugin::PLUGIN_NAME = "ScreenshotPlugin";


/////////////////////////////////////////////////////
// Constructors/ Destructors
/////////////////////////////////////////////////////

ScreenshotPlugin::ScreenshotPlugin(const ScreenshotPluginContainer& container) :
	_container(container)
{ 
	_pProvider = 0;
	_pReply = 0;
}

ScreenshotPlugin::~ScreenshotPlugin() 
{ 
}

/////////////////////////////////////////////////////
// Plugin Interface
/////////////////////////////////////////////////////

void ScreenshotPlugin::init(IProvider* pProvider)
{
	_pProvider = pProvider;
	QMainWindow* pWindow = pProvider->mainWindow();
	_pScrollArea = new QScrollArea(pWindow);
	_pScreenshotWidget = new QLabel("", pWindow);
	_pScrollArea->setWidget(_pScreenshotWidget);
	_pScrollArea->setWidgetResizable(true);
	//_pScrollArea->setVisible(false);
}


QString ScreenshotPlugin::title() const 
{ 
	return tr("Screenshot Plugin"); 
}

QString ScreenshotPlugin::briefDescription() const 
{ 
	return tr("Displays screenshots of the packages to be installed"); 
}

QString ScreenshotPlugin::description() const 
{ 
	return tr("This plugin displays screenshots of the packages to be installed. "
		"It fetches the screenshots from http://screenshots.debian.net so a working "
		"internet connection is required."); 
}

/////////////////////////////////////////////////////
// Information Plugin Interface 
/////////////////////////////////////////////////////

QWidget* ScreenshotPlugin::informationWidget() const	
{
	return _pScrollArea; 
}

void ScreenshotPlugin::updateInformationWidget(const string& package) 
{
/*	qDebug() << "Label size: " << _pScreenshotWidget->size();
	qDebug() << "Scroll size: " << _pScrollArea->size();*/
	if (_pReply) // if we have a download running, delete it
	{
		abortDownload();
	}


	_pScreenshotWidget->setText(tr("Loading screenshot\nEstablishing connection, please wait..."));
	//_pReply = _nam.get(QNetworkRequest("myfile"));

	QUrl url(QString("http://screenshots.debian.net/screenshot/")+toQString(package));
	_pReply = _pProvider->network()->get(QNetworkRequest(url));
	connect(_pReply, SIGNAL(finished()), SLOT(httpFinished()));
	connect(_pReply, SIGNAL(error(QNetworkReply::NetworkError)), 
			SLOT(httpError(QNetworkReply::NetworkError)));
	connect(_pReply, SIGNAL(downloadProgress(qint64,qint64)),
			SLOT(httpDownloadProgress(qint64,qint64)));
	
}


/////////////////////////////////////////////////////
// Helper Methods
/////////////////////////////////////////////////////



void ScreenshotPlugin::abortDownload()
{
	if (_pReply) 
	{
		_pReply->abort();
		_pReply->deleteLater();
		_pReply = 0;
	}
}

void ScreenshotPlugin::httpFinished() 
{
	// if an error other than ContentNotFound (which is expected if no screenshot is available) occured 
	if (_pReply->error() != QNetworkReply::NoError && _pReply->error() != QNetworkReply::ContentNotFoundError)
		return;
	QImageReader reader(_pReply);
	QImage image = reader.read();
	_pReply->deleteLater();
	_pReply = 0;
	_pScreenshotWidget->setPixmap(QPixmap::fromImage(image));
	
}

void ScreenshotPlugin::httpDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
	// if an error occured or the download is finished
	if (_pReply->error() != QNetworkReply::NoError || _pReply->isFinished())
		return;
	if (bytesTotal > 10*1024*1024) 
	{
		abortDownload();
		_pScreenshotWidget->setText(tr("The screenshot size exceeds 10 MB. "
				"Something seems to be wrong here!\n Aborting Download."));
		return;
	}
	QString progress;
	progress.setNum((int) (((float) bytesReceived)/bytesTotal*100.0f) ) ;
	// ToDo insert linebreak instead of " - " here, but  currently this leads to loads of QT warnings
	_pScreenshotWidget->setText(tr("Loading screenshot - Progress: ") + progress + "%");
}

void ScreenshotPlugin::httpError(QNetworkReply::NetworkError e) 
{
	// this error is normally returned by if there was no screenshot available
	if (e==QNetworkReply::ContentNotFoundError) 
	{
		_pScreenshotWidget->setText(tr("No screenshot available for ") + _pReply->url().toString());
		return;
	}
	// if the error is OperationCanceled that is no error
	if (e==QNetworkReply::OperationCanceledError) 
		return;
	qDebug() << "Network error\n" << _pReply->errorString();
	// in all other cases abort the download

	_pScreenshotWidget->setText(tr("An error occured while trying to download the screenshot:\n") +
			_pReply->errorString());
	abortDownload();
}



}	// namespace NPlugin