summaryrefslogtreecommitdiffstats
path: root/src/gvcore/thumbnailloadjob.h
blob: 283d8020954956e3cec979df9e5386de0ba46e61 (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
/*	Gwenview - A simple image viewer for TDE
	Copyright 2000-2004 Aur�lien G�teau
	This class is based on the ImagePreviewJob class from Konqueror.
	Original copyright follows.
*/
/*	This file is part of the KDE project
	Copyright (C) 2000 David Faure <faure@kde.org>

	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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef THUMBNAILLOADJOB_H
#define THUMBNAILLOADJOB_H

// TQt
#include <tqimage.h>
#include <tqpixmap.h>
#include <tqvaluelist.h>
#include <tqvaluevector.h>

// KDE
#include <tdeio/job.h>

// Local
#include "tsthread/tsthread.h"
#include "tsthread/tswaitcondition.h"

#include "libgwenview_export.h"
class TDEConfig;
class KFileItem;

typedef TQPtrList<KFileItem> KFileItemList;

namespace Gwenview {
class ThumbnailThread : public TSThread {
Q_OBJECT
  
public:
	void load(
		const TQString& originalURI,
		time_t originalTime,
		int originalSize,
		const TQString& originalMimeType,
		const TQString& pixPath,
		const TQString& thumbnailPath,
		int size,
		bool storeThumbnail);
protected:
	virtual void run();
signals:
	void done( const TQImage&, const TQSize&);
private:
	bool isJPEG();
	bool loadJPEG();
	void loadThumbnail();
	TQImage mImage;
	TQString mPixPath;
	TQString mThumbnailPath;
	TQString mOriginalURI;
	time_t mOriginalTime;
	int mOriginalSize;
	TQString mOriginalMimeType;
	int mOriginalWidth;
	int mOriginalHeight;
	TQMutex mMutex;
	TSWaitCondition mCond;
	int mThumbnailSize;
	bool mStoreThumbnailsInCache;
};

/**
 * A job that determines the thumbnails for the images in the current directory
 */
class LIBGWENVIEW_EXPORT ThumbnailLoadJob : public TDEIO::Job {
Q_OBJECT
  
public:
	/**
	 * Create a job for determining the pixmaps of the images in the @p itemList
	 */
	ThumbnailLoadJob(const TQValueVector<const KFileItem*>* itemList, int size);
	virtual ~ThumbnailLoadJob();

	/**
	 * Call this to get started
	 */
	void start();

	/**
	 * To be called whenever an item is removed from the view
	 */
	void itemRemoved(const KFileItem* item);


	/**
	 * Add an item to a running job
	 */
	void appendItem(const KFileItem* item);

	
	/**
	 * Sets items in range first..last to be generated first, starting with current.
	 */	
	void setPriorityItems(const KFileItem* current, const KFileItem* first, const KFileItem* last);

	/**
	 * Temporarily suspends loading. Used if there's a more
	 * important action going on (loading an image etc.).
	 */
	void suspend();

	/**
	 * Resumes loading if suspended.
	 */
	void resume();

	/**
	 * Returns the thumbnail base dir, independent of the thumbnail size
	 */
	static TQString thumbnailBaseDir();
	
	/**
	 * Returns the thumbnail base dir, for the @p size
	 */
	static TQString thumbnailBaseDir(int size);


	/**
	 * Delete the thumbnail for the @p url
	 */
	static void deleteImageThumbnail(const KURL& url);

signals:
	/**
	 * Emitted when the thumbnail for the @p item has been loaded
	 */
	void thumbnailLoaded(const KFileItem* item, const TQPixmap&, const TQSize&);

private slots:
	void slotResult( TDEIO::Job *job );
	void slotGotPreview(const KFileItem*, const TQPixmap&);
	void checkThumbnail();
	void thumbnailReady(const TQImage& im, const TQSize&);
	void emitThumbnailLoadingFailed();

private:
	enum { STATE_STATORIG, STATE_DOWNLOADORIG, STATE_PREVIEWJOB, STATE_NEXTTHUMB } mState;

	TQValueList<const KFileItem*> mItems;
	TQValueVector<const KFileItem* > mAllItems;
	TQValueVector< bool > mProcessedState;
	const KFileItem *mCurrentItem;
	int thumbnailIndex( const KFileItem* item ) const;
	void updateItemsOrder();

	// indexes of the current, fist and last visible thumbnails
	int mCurrentVisibleIndex, mFirstVisibleIndex, mLastVisibleIndex;

	// The URL of the current item (always equivalent to m_items.first()->item()->url())
	KURL mCurrentURL;

	// The URI of the original image (might be different from mCurrentURL.url())
	TQString mOriginalURI;
	
	// The modification time of the original image
	time_t mOriginalTime;

	// The thumbnail path
	TQString mThumbnailPath;

	// The temporary path for remote urls
	TQString mTempPath;

	// Thumbnail size
	int mThumbnailSize;

	TQPixmap mBrokenPixmap;

	bool mSuspended;

	ThumbnailThread mThumbnailThread;

	void determineNextIcon();
	void startCreatingThumbnail(const TQString& path);
	
	void emitThumbnailLoaded(const TQImage& img, TQSize size);

	void updateItemsOrderHelper( int forward, int backward, int first, int last );
};

inline
int ThumbnailLoadJob::thumbnailIndex( const KFileItem* item ) const {
	TQValueVector<const KFileItem* >::ConstIterator pos = tqFind( mAllItems.begin(), mAllItems.end(), item );
	if( pos != mAllItems.end()) return pos - mAllItems.begin();
	return -1;
}

} // namespace
#endif