1/***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20#ifndef DOLPHINVIEWCONTAINER_H
21#define DOLPHINVIEWCONTAINER_H
22
23#include <KFileItem>
24#include <KFileItemDelegate>
25#include <KGlobalSettings>
26#include <KIO/Job>
27
28#include <KUrlNavigator>
29
30#include <QElapsedTimer>
31#include <QWidget>
32
33#include <views/dolphinview.h>
34#include <config-apps.h>
35
36#ifdef KActivities_FOUND
37namespace KActivities {
38 class ResourceInstance;
39}
40#endif
41
42class FilterBar;
43class KMessageWidget;
44class KUrl;
45class KUrlNavigator;
46class DolphinSearchBox;
47class DolphinStatusBar;
48
49/**
50 * @short Represents a view for the directory content
51 * including the navigation bar, filter bar and status bar.
52 *
53 * View modes for icons, compact and details are supported. Currently
54 * Dolphin allows to have up to two views inside the main window.
55 *
56 * @see DolphinView
57 * @see FilterBar
58 * @see KUrlNavigator
59 * @see DolphinStatusBar
60 */
61class DolphinViewContainer : public QWidget
62{
63 Q_OBJECT
64
65public:
66 enum MessageType
67 {
68 Information,
69 Warning,
70 Error
71 };
72
73 DolphinViewContainer(const KUrl& url, QWidget* parent);
74 virtual ~DolphinViewContainer();
75
76 /**
77 * Returns the current active URL, where all actions are applied.
78 * The URL navigator is synchronized with this URL.
79 */
80 KUrl url() const;
81
82 /**
83 * If \a active is true, the view container will marked as active. The active
84 * view container is defined as view where all actions are applied to.
85 */
86 void setActive(bool active);
87 bool isActive() const;
88
89 /**
90 * If \a grab is set to true, the container automatically grabs the focus
91 * as soon as the URL has been changed. Per default the grabbing
92 * of the focus is enabled.
93 */
94 void setAutoGrabFocus(bool grab);
95 bool autoGrabFocus() const;
96
97 const DolphinStatusBar* statusBar() const;
98 DolphinStatusBar* statusBar();
99
100 const KUrlNavigator* urlNavigator() const;
101 KUrlNavigator* urlNavigator();
102
103 const DolphinView* view() const;
104 DolphinView* view();
105
106 /**
107 * Shows the message \msg with the given type non-modal above
108 * the view-content.
109 */
110 void showMessage(const QString& msg, MessageType type);
111
112 /**
113 * Refreshes the view container to get synchronized with the (updated) Dolphin settings.
114 */
115 void readSettings();
116
117 /** Returns true, if the filter bar is visible. */
118 bool isFilterBarVisible() const;
119
120 /**
121 * Enables the search mode, if \p enabled is true. In the search mode the URL navigator
122 * will be hidden and replaced by a line editor that allows to enter a search term.
123 */
124 void setSearchModeEnabled(bool enabled);
125 bool isSearchModeEnabled() const;
126
127 /**
128 * @return Text that should be used for the current URL when creating
129 * a new place.
130 */
131 QString placesText() const;
132
133public slots:
134 /**
135 * Sets the current active URL, where all actions are applied. The
136 * URL navigator is synchronized with this URL. The signals
137 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
138 * are emitted.
139 * @see DolphinViewContainer::urlNavigator()
140 */
141 void setUrl(const KUrl& url);
142
143 /**
144 * Popups the filter bar above the status bar if \a visible is true.
145 * It \a visible is true, it is assured that the filter bar gains
146 * the keyboard focus.
147 */
148 void setFilterBarVisible(bool visible);
149
150signals:
151 /**
152 * Is emitted whenever the filter bar has changed its visibility state.
153 */
154 void showFilterBarChanged(bool shown);
155
156 /**
157 * Is emitted when the write state of the folder has been changed. The application
158 * should disable all actions like "Create New..." that depend on the write
159 * state.
160 */
161 void writeStateChanged(bool isFolderWritable);
162
163private slots:
164 /**
165 * Updates the number of items (= number of files + number of
166 * directories) in the statusbar. If files are selected, the number
167 * of selected files and the sum of the filesize is shown. The update
168 * is done asynchronously, as getting the sum of the
169 * filesizes can be an expensive operation.
170 * Unless a previous OperationCompletedMessage was set very shortly before
171 * calling this method, it will be overwritten (see DolphinStatusBar::setMessage).
172 * Previous ErrorMessages however are always preserved.
173 */
174 void delayedStatusBarUpdate();
175
176 /**
177 * Is invoked by DolphinViewContainer::delayedStatusBarUpdate() and
178 * updates the status bar synchronously.
179 */
180 void updateStatusBar();
181
182 void updateDirectoryLoadingProgress(int percent);
183
184 void updateDirectorySortingProgress(int percent);
185
186 /**
187 * Updates the statusbar to show an undetermined progress with the correct
188 * context information whether a searching or a directory loading is done.
189 */
190 void slotDirectoryLoadingStarted();
191
192 /**
193 * Assures that the viewport position is restored and updates the
194 * statusbar to reflect the current content.
195 */
196 void slotDirectoryLoadingCompleted();
197
198 /**
199 * Updates the statusbar to show, that the directory loading has
200 * been canceled.
201 */
202 void slotDirectoryLoadingCanceled();
203
204 /**
205 * Is called if the URL set by DolphinView::setUrl() represents
206 * a file and not a directory. Takes care to activate the file.
207 */
208 void slotUrlIsFileError(const KUrl& url);
209
210 /**
211 * Handles clicking on an item. If the item is a directory, the
212 * directory is opened in the view. If the item is a file, the file
213 * gets started by the corresponding application.
214 */
215 void slotItemActivated(const KFileItem& item);
216
217 /**
218 * Handles activation of multiple files. The files get started by
219 * the corresponding applications.
220 */
221 void slotItemsActivated(const KFileItemList& items);
222
223 /**
224 * Shows the information for the item \a item inside the statusbar. If the
225 * item is null, the default statusbar information is shown.
226 */
227 void showItemInfo(const KFileItem& item);
228
229 void closeFilterBar();
230
231 /**
232 * Filters the currently shown items by \a nameFilter. All items
233 * which contain the given filter string will be shown.
234 */
235 void setNameFilter(const QString& nameFilter);
236
237 /**
238 * Marks the view container as active
239 * (see DolphinViewContainer::setActive()).
240 */
241 void activate();
242
243 /**
244 * Is invoked if the signal urlAboutToBeChanged() from the DolphinView
245 * is emitted. Tries to save the view-state.
246 */
247 void slotViewUrlAboutToBeChanged(const KUrl& url);
248
249 /**
250 * Is invoked if the signal urlAboutToBeChanged() from the URL navigator
251 * is emitted. Tries to save the view-state.
252 */
253 void slotUrlNavigatorLocationAboutToBeChanged(const KUrl& url);
254
255 /**
256 * Restores the current view to show \a url and assures
257 * that the root URL of the view is respected.
258 */
259 void slotUrlNavigatorLocationChanged(const KUrl& url);
260
261 /**
262 * Is connected with the URL navigator and drops the URLs
263 * above the destination \a destination.
264 *
265 * Creates a copy of \a event and invokes \a dropUrlsDelayed with a
266 * queued connection.
267 */
268 void dropUrls(const KUrl& destination, QDropEvent* event);
269
270 /**
271 * Is invoked with a queued connection by \a dropUrls to prevent that the
272 * drop actions are executed in the URL navigator menu's nested event loop,
273 * which might cause a crash. Simply using a queued connection from the URL
274 * navigator to \a dropUrls would not work because the \a event pointer
275 * would be dangling then.
276 */
277 void dropUrlsDelayed();
278
279 /**
280 * Is invoked when a redirection is done and changes the
281 * URL of the URL navigator to \a newUrl without triggering
282 * a reloading of the directory.
283 */
284 void redirect(const KUrl& oldUrl, const KUrl& newUrl);
285
286 /** Requests the focus for the view \a m_view. */
287 void requestFocus();
288
289 /**
290 * Saves the currently used URL completion mode of
291 * the URL navigator.
292 */
293 void saveUrlCompletionMode(KGlobalSettings::Completion completion);
294
295 void slotHistoryChanged();
296
297 void slotReturnPressed();
298
299 /**
300 * Gets the search URL from the searchbox and starts searching.
301 */
302 void startSearching();
303 void closeSearchBox();
304
305 /**
306 * Stops the loading of a directory. Is connected with the "stopPressed" signal
307 * from the statusbar.
308 */
309 void stopDirectoryLoading();
310
311 void slotStatusBarZoomLevelChanged(int zoomLevel);
312
313 /**
314 * Slot that calls showMessage(msg, Error).
315 */
316 void showErrorMessage(const QString& msg);
317
318private:
319 /**
320 * @return True if the URL protocol is a search URL (e. g. baloosearch:// or filenamesearch://).
321 */
322 bool isSearchUrl(const KUrl& url) const;
323
324 /**
325 * Saves the state of the current view: contents position,
326 * root URL, ...
327 */
328 void saveViewState();
329
330private:
331 QVBoxLayout* m_topLayout;
332 KUrlNavigator* m_urlNavigator;
333 DolphinSearchBox* m_searchBox;
334 KMessageWidget* m_messageWidget;
335
336 DolphinView* m_view;
337
338 FilterBar* m_filterBar;
339
340 DolphinStatusBar* m_statusBar;
341 QTimer* m_statusBarTimer; // Triggers a delayed update
342 QElapsedTimer m_statusBarTimestamp; // Time in ms since last update
343 bool m_autoGrabFocus;
344
345 KUrl m_dropDestination;
346 QScopedPointer<QDropEvent> m_dropEvent;
347
348#ifdef KActivities_FOUND
349private:
350 KActivities::ResourceInstance * m_activityResourceInstance;
351#endif
352};
353
354#endif // DOLPHINVIEWCONTAINER_H
355