summaryrefslogtreecommitdiffstats
path: root/src/gvcore/slideshow.cpp
blob: fb9442a1c172f5ea017570148ac08caa3e7a96cb (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// vim: set tabstop=4 shiftwidth=4 noexpandtab
// kate: indent-mode csands; indent-width 4; replace-tabs-save off; replace-tabs off; replace-trailing-space-save off; space-indent off; tabs-indents on; tab-width 4;
/*
Gwenview - A simple image viewer for KDE
Copyright 2000-2004 Aur�ien G�eau

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 program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

// STL
#include <algorithm>

// Qt
#include <qtimer.h>

// KDE
#include <kconfig.h>
#include <kdebug.h>

// Local
#include <../gvcore/slideshowconfig.h>
#include "slideshow.moc"

#include "document.h"
#include "imageloader.h"
#include "cache.h"

namespace Gwenview {

#undef ENABLE_LOG
#undef LOG
//#define ENABLE_LOG
#ifdef ENABLE_LOG
#define LOG(x) kdDebug() << k_funcinfo << x << endl
#else
#define LOG(x) ;
#endif

SlideShow::SlideShow(Document* document)
: mDocument(document), mStarted(false), mPrefetch( NULL ) {
	mTimer=new QTimer(this);
	connect(mTimer, SIGNAL(timeout()),
			this, SLOT(slotTimeout()) );
	connect(mDocument, SIGNAL(loaded(const KURL&)),
			this, SLOT(slotLoaded()) );
}

SlideShow::~SlideShow() {
	if( !mPriorityURL.isEmpty()) Cache::instance()->setPriorityURL( mPriorityURL, false );
}


void SlideShow::slotSettingsChanged() {
	if (mTimer->isActive()) {
		mTimer->changeInterval(timerInterval());
	}
}


int SlideShow::timerInterval() {
	int documentDuration = mDocument->duration();
	if (documentDuration != 0) {
		return documentDuration * 1000;
	} else {
		return int(SlideShowConfig::delay()*1000);
	}
}


void SlideShow::start(const KURL::List& urls) {
	mURLs.resize(urls.size());
	qCopy(urls.begin(), urls.end(), mURLs.begin());
	if (SlideShowConfig::random()) {
		std::random_shuffle(mURLs.begin(), mURLs.end());
	}

	mStartIt=qFind(mURLs.begin(), mURLs.end(), mDocument->url());
	if (mStartIt==mURLs.end()) {
		kdWarning() << k_funcinfo << "Current URL not found in list, aborting.\n";
		return;
	}
	
	mTimer->start(timerInterval(), true);
	mStarted=true;
	prefetch();
	emit stateChanged(true);
}


void SlideShow::stop() {
	mTimer->stop();
	mStarted=false;
	emit stateChanged(false);
	if( !mPriorityURL.isEmpty()) {
		Cache::instance()->setPriorityURL( mPriorityURL, false );
		mPriorityURL = KURL();
	}
}


QValueVector<KURL>::ConstIterator SlideShow::findNextURL() const {
	QValueVector<KURL>::ConstIterator it=qFind(mURLs.begin(), mURLs.end(), mDocument->url());
	if (it==mURLs.end()) {
		kdWarning() << k_funcinfo << "Current URL not found in list. This should not happen.\n";
		return it;
	}

	++it;
	if (SlideShowConfig::loop()) {
		// Looping, if we reach the end, start again
		if (it==mURLs.end()) {
			it = mURLs.begin();
		}
	} else {
		// Not looping, have we reached the end?
		if ((it==mURLs.end() && SlideShowConfig::stopAtEnd()) || it==mStartIt) {
			it = mURLs.end();
		}
	}

	return it;
}


void SlideShow::slotTimeout() {
	LOG("");
	// wait for prefetching to finish
	if( mPrefetch != NULL ) {
		LOG("mPrefetch is working");
		return;
	}

	QValueVector<KURL>::ConstIterator it=findNextURL();
	if (it==mURLs.end()) {
		stop();
		return;
	}
	emit nextURL(*it);
}


void SlideShow::slotLoaded() {
	if (mStarted) {
		mTimer->start(timerInterval(), true);
		prefetch();
	}
}


void SlideShow::prefetch() {
	LOG("");
	QValueVector<KURL>::ConstIterator it=findNextURL();
	if (it==mURLs.end()) {
		return;
	}
	LOG("url=" << (*it).pathOrURL());

	if( mPrefetch != NULL ) mPrefetch->release( this );
	// TODO don't use prefetching with disabled optimizations (and add that option ;) )
	// (and also don't use prefetching in other places if the image won't fit in cache)
	mPrefetch = ImageLoader::loader( *it, this, BUSY_PRELOADING );
	if( !mPriorityURL.isEmpty()) Cache::instance()->setPriorityURL( mPriorityURL, false );
	mPriorityURL = *it;
	Cache::instance()->setPriorityURL( mPriorityURL, true ); // make sure it will stay in the cache
	connect( mPrefetch, SIGNAL( urlKindDetermined()), SLOT( slotUrlKindDetermined()));
	connect( mPrefetch, SIGNAL( imageLoaded( bool )), SLOT( prefetchDone()));
	
	if (mPrefetch->urlKind()==MimeTypeUtils::KIND_FILE) {
		// Prefetch is already done, and this is not a raster image
		prefetchDone();
	}
}

void SlideShow::slotUrlKindDetermined() {
	LOG("");
	if (!mPrefetch) return;
	
	LOG("mPrefetch!=0");
	if (mPrefetch->urlKind()==MimeTypeUtils::KIND_FILE) {
		LOG("KIND_FILE");
		// This is not a raster image, imageLoaded will not be emitted
		prefetchDone();
	}
}


void SlideShow::prefetchDone() {
	LOG("");
	if( mPrefetch != NULL ) { 
		LOG("mPrefetch!=0");
		// don't call Cache::setPriorityURL( ... , false ) here - it will still take
		// a short while to reuse the image from the cache
		mPrefetch->release( this );
		mPrefetch = NULL;
		// prefetching completed and delay has already elapsed
		if( mStarted && !mTimer->isActive()) {
			LOG("Calling slotTimeout");
			slotTimeout();
		}
	}
}


} // namespace