OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <gdk/gdkx.h> | 5 #include <gdk/gdkx.h> |
6 #include <gtk/gtk.h> | 6 #include <gtk/gtk.h> |
7 #include <map> | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/file_util.h" | |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/message_loop.h" | |
16 #include "base/mime_util.h" | 14 #include "base/mime_util.h" |
| 15 #include "base/nix/xdg_util.h" |
17 #include "base/process_util.h" | 16 #include "base/process_util.h" |
18 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
19 #include "base/string_util.h" | 18 #include "base/string_util.h" |
20 #include "base/sys_string_conversions.h" | |
21 #include "base/threading/thread.h" | |
22 #include "base/threading/thread_restrictions.h" | |
23 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
24 #include "chrome/browser/ui/gtk/dialogs_common.h" | 20 #include "chrome/browser/ui/gtk/dialogs_common.h" |
25 #include "chrome/browser/ui/shell_dialogs.h" | |
26 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
27 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
28 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
29 | 24 |
30 using content::BrowserThread; | 25 using content::BrowserThread; |
31 | 26 |
| 27 namespace { |
| 28 |
| 29 std::string GetTitle(const std::string& title, int message_id) { |
| 30 return title.empty() ? l10n_util::GetStringUTF8(message_id) : title; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
32 // Implementation of SelectFileDialog that shows a KDE common dialog for | 35 // Implementation of SelectFileDialog that shows a KDE common dialog for |
33 // choosing a file or folder. This acts as a modal dialog. | 36 // choosing a file or folder. This acts as a modal dialog. |
34 class SelectFileDialogImplKDE : public SelectFileDialogImpl { | 37 class SelectFileDialogImplKDE : public SelectFileDialogImpl { |
35 public: | 38 public: |
36 explicit SelectFileDialogImplKDE(Listener* listener); | 39 SelectFileDialogImplKDE(Listener* listener, |
| 40 base::nix::DesktopEnvironment desktop); |
37 ~SelectFileDialogImplKDE(); | 41 ~SelectFileDialogImplKDE(); |
38 | 42 |
39 protected: | 43 protected: |
40 // SelectFileDialog implementation. | 44 // SelectFileDialog implementation. |
41 // |params| is user data we pass back via the Listener interface. | 45 // |params| is user data we pass back via the Listener interface. |
42 virtual void SelectFileImpl(Type type, | 46 virtual void SelectFileImpl(Type type, |
43 const string16& title, | 47 const string16& title, |
44 const FilePath& default_path, | 48 const FilePath& default_path, |
45 const FileTypeInfo* file_types, | 49 const FileTypeInfo* file_types, |
46 int file_type_index, | 50 int file_type_index, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 void SelectSingleFileHelper(const std::string& output, int exit_code, | 103 void SelectSingleFileHelper(const std::string& output, int exit_code, |
100 void* params, bool allow_folder); | 104 void* params, bool allow_folder); |
101 | 105 |
102 void OnSelectSingleFileDialogResponse(const std::string& output, | 106 void OnSelectSingleFileDialogResponse(const std::string& output, |
103 int exit_code, void* params); | 107 int exit_code, void* params); |
104 void OnSelectMultiFileDialogResponse(const std::string& output, | 108 void OnSelectMultiFileDialogResponse(const std::string& output, |
105 int exit_code, void* params); | 109 int exit_code, void* params); |
106 void OnSelectSingleFolderDialogResponse(const std::string& output, | 110 void OnSelectSingleFolderDialogResponse(const std::string& output, |
107 int exit_code, void* params); | 111 int exit_code, void* params); |
108 | 112 |
| 113 // Should be either DESKTOP_ENVIRONMENT_KDE3 or DESKTOP_ENVIRONMENT_KDE4. |
| 114 base::nix::DesktopEnvironment desktop_; |
| 115 |
109 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImplKDE); | 116 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImplKDE); |
110 }; | 117 }; |
111 | 118 |
| 119 // static |
112 SelectFileDialogImpl* SelectFileDialogImpl::NewSelectFileDialogImplKDE( | 120 SelectFileDialogImpl* SelectFileDialogImpl::NewSelectFileDialogImplKDE( |
113 Listener* listener) { | 121 Listener* listener, base::nix::DesktopEnvironment desktop) { |
114 return new SelectFileDialogImplKDE(listener); | 122 return new SelectFileDialogImplKDE(listener, desktop); |
115 } | 123 } |
116 | 124 |
117 SelectFileDialogImplKDE::SelectFileDialogImplKDE(Listener* listener) | 125 SelectFileDialogImplKDE::SelectFileDialogImplKDE( |
118 : SelectFileDialogImpl(listener) { | 126 Listener* listener, |
| 127 base::nix::DesktopEnvironment desktop) |
| 128 : SelectFileDialogImpl(listener), |
| 129 desktop_(desktop) { |
| 130 DCHECK(desktop_ == base::nix::DESKTOP_ENVIRONMENT_KDE3 || |
| 131 desktop_ == base::nix::DESKTOP_ENVIRONMENT_KDE4); |
119 } | 132 } |
120 | 133 |
121 SelectFileDialogImplKDE::~SelectFileDialogImplKDE() { | 134 SelectFileDialogImplKDE::~SelectFileDialogImplKDE() { |
122 } | 135 } |
123 | 136 |
124 // We ignore |default_extension|. | 137 // We ignore |default_extension|. |
125 void SelectFileDialogImplKDE::SelectFileImpl( | 138 void SelectFileDialogImplKDE::SelectFileImpl( |
126 Type type, | 139 Type type, |
127 const string16& title, | 140 const string16& title, |
128 const FilePath& default_path, | 141 const FilePath& default_path, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 240 |
228 void SelectFileDialogImplKDE::GetKDialogCommandLine(const std::string& type, | 241 void SelectFileDialogImplKDE::GetKDialogCommandLine(const std::string& type, |
229 const std::string& title, const FilePath& path, | 242 const std::string& title, const FilePath& path, |
230 gfx::NativeWindow parent, bool file_operation, bool multiple_selection, | 243 gfx::NativeWindow parent, bool file_operation, bool multiple_selection, |
231 CommandLine* command_line) { | 244 CommandLine* command_line) { |
232 if (!command_line) { | 245 if (!command_line) { |
233 VLOG(1) << "Command line for KDialog is NULL" << std::endl; | 246 VLOG(1) << "Command line for KDialog is NULL" << std::endl; |
234 return; | 247 return; |
235 } | 248 } |
236 // Attach to the current Chrome window. | 249 // Attach to the current Chrome window. |
237 GdkWindow* gdk_window = | 250 GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET((parent))); |
238 gtk_widget_get_window(GTK_WIDGET((parent))); | |
239 int window_id = GDK_DRAWABLE_XID(gdk_window); | 251 int window_id = GDK_DRAWABLE_XID(gdk_window); |
240 command_line->AppendSwitchNative("--attach", base::IntToString(window_id)); | 252 command_line->AppendSwitchNative( |
| 253 desktop_ == base::nix::DESKTOP_ENVIRONMENT_KDE3 ? "--embed" : "--attach", |
| 254 base::IntToString(window_id)); |
241 // Set the correct title for the dialog. | 255 // Set the correct title for the dialog. |
242 if (!title.empty()) { | 256 if (!title.empty()) |
243 command_line->AppendSwitchNative("--title", title); | 257 command_line->AppendSwitchNative("--title", title); |
244 } | |
245 // Enable multiple file selection if we need to. | 258 // Enable multiple file selection if we need to. |
246 if (multiple_selection) { | 259 if (multiple_selection) { |
247 command_line->AppendSwitch("--multiple"); | 260 command_line->AppendSwitch("--multiple"); |
248 command_line->AppendSwitch("--separate-output"); | 261 command_line->AppendSwitch("--separate-output"); |
249 } | 262 } |
250 command_line->AppendSwitch(type); | 263 command_line->AppendSwitch(type); |
251 // The path should never be empty. If it is, set it to PWD. | 264 // The path should never be empty. If it is, set it to PWD. |
252 if (path.empty()) { | 265 if (path.empty()) |
253 command_line->AppendArgPath(FilePath(".")); | 266 command_line->AppendArgPath(FilePath(".")); |
254 } else { | 267 else |
255 command_line->AppendArgPath(path); | 268 command_line->AppendArgPath(path); |
256 } | |
257 // Depending on the type of the operation we need, get the path to the | 269 // Depending on the type of the operation we need, get the path to the |
258 // file/folder and set up mime type filters. | 270 // file/folder and set up mime type filters. |
259 if (file_operation) { | 271 if (file_operation) |
260 command_line->AppendArg(GetMimeTypeFilterString()); | 272 command_line->AppendArg(GetMimeTypeFilterString()); |
261 } | |
262 VLOG(1) << "KDialog command line: " | 273 VLOG(1) << "KDialog command line: " |
263 << command_line->GetCommandLineString() << std::endl; | 274 << command_line->GetCommandLineString() << std::endl; |
264 } | 275 } |
265 | 276 |
266 void SelectFileDialogImplKDE::FileSelected(const FilePath& path, void* params) { | 277 void SelectFileDialogImplKDE::FileSelected(const FilePath& path, void* params) { |
267 if (type_ == SELECT_SAVEAS_FILE) | 278 if (type_ == SELECT_SAVEAS_FILE) |
268 *last_saved_path_ = path.DirName(); | 279 *last_saved_path_ = path.DirName(); |
269 else if (type_ == SELECT_OPEN_FILE) | 280 else if (type_ == SELECT_OPEN_FILE) |
270 *last_opened_path_ = path.DirName(); | 281 *last_opened_path_ = path.DirName(); |
271 else if (type_ == SELECT_FOLDER) | 282 else if (type_ == SELECT_FOLDER) |
(...skipping 14 matching lines...) Expand all Loading... |
286 } | 297 } |
287 | 298 |
288 void SelectFileDialogImplKDE::FileNotSelected(void* params) { | 299 void SelectFileDialogImplKDE::FileNotSelected(void* params) { |
289 if (listener_) | 300 if (listener_) |
290 listener_->FileSelectionCanceled(params); | 301 listener_->FileSelectionCanceled(params); |
291 } | 302 } |
292 | 303 |
293 void SelectFileDialogImplKDE::CreateSelectFolderDialog( | 304 void SelectFileDialogImplKDE::CreateSelectFolderDialog( |
294 const std::string& title, const FilePath& default_path, | 305 const std::string& title, const FilePath& default_path, |
295 gfx::NativeWindow parent, void *params) { | 306 gfx::NativeWindow parent, void *params) { |
296 std::string title_string = !title.empty() ? title : | |
297 l10n_util::GetStringUTF8(IDS_SELECT_FOLDER_DIALOG_TITLE); | |
298 | |
299 Task* dialog_task = | 307 Task* dialog_task = |
300 NewRunnableMethod( | 308 NewRunnableMethod( |
301 this, &SelectFileDialogImplKDE::CallKDialogOutput, | 309 this, &SelectFileDialogImplKDE::CallKDialogOutput, |
302 std::string("--getexistingdirectory"), title, | 310 std::string("--getexistingdirectory"), |
| 311 GetTitle(title, IDS_SELECT_FOLDER_DIALOG_TITLE), |
303 default_path.empty() ? *last_opened_path_ : default_path, | 312 default_path.empty() ? *last_opened_path_ : default_path, |
304 parent, false, false, params, | 313 parent, false, false, params, |
305 &SelectFileDialogImplKDE::OnSelectSingleFolderDialogResponse); | 314 &SelectFileDialogImplKDE::OnSelectSingleFolderDialogResponse); |
306 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, dialog_task); | 315 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, dialog_task); |
307 } | 316 } |
308 | 317 |
309 void SelectFileDialogImplKDE::CreateFileOpenDialog( | 318 void SelectFileDialogImplKDE::CreateFileOpenDialog( |
310 const std::string& title, const FilePath& default_path, | 319 const std::string& title, const FilePath& default_path, |
311 gfx::NativeWindow parent, void* params) { | 320 gfx::NativeWindow parent, void* params) { |
312 std::string title_string = !title.empty() ? title : | |
313 l10n_util::GetStringUTF8(IDS_OPEN_FILE_DIALOG_TITLE); | |
314 | |
315 Task* dialog_task = | 321 Task* dialog_task = |
316 NewRunnableMethod( | 322 NewRunnableMethod( |
317 this, &SelectFileDialogImplKDE::CallKDialogOutput, | 323 this, &SelectFileDialogImplKDE::CallKDialogOutput, |
318 std::string("--getopenfilename"), title, | 324 std::string("--getopenfilename"), |
| 325 GetTitle(title, IDS_OPEN_FILE_DIALOG_TITLE), |
319 default_path.empty() ? *last_opened_path_ : default_path, | 326 default_path.empty() ? *last_opened_path_ : default_path, |
320 parent, true, false, params, | 327 parent, true, false, params, |
321 &SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse); | 328 &SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse); |
322 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, dialog_task); | 329 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, dialog_task); |
323 } | 330 } |
324 | 331 |
325 void SelectFileDialogImplKDE::CreateMultiFileOpenDialog( | 332 void SelectFileDialogImplKDE::CreateMultiFileOpenDialog( |
326 const std::string& title, const FilePath& default_path, | 333 const std::string& title, const FilePath& default_path, |
327 gfx::NativeWindow parent, void* params) { | 334 gfx::NativeWindow parent, void* params) { |
328 std::string title_string = !title.empty() ? title : | |
329 l10n_util::GetStringUTF8(IDS_OPEN_FILES_DIALOG_TITLE); | |
330 | |
331 Task* dialog_task = | 335 Task* dialog_task = |
332 NewRunnableMethod( | 336 NewRunnableMethod( |
333 this, &SelectFileDialogImplKDE::CallKDialogOutput, | 337 this, &SelectFileDialogImplKDE::CallKDialogOutput, |
334 std::string("--getopenfilename"), title, | 338 std::string("--getopenfilename"), |
| 339 GetTitle(title, IDS_OPEN_FILES_DIALOG_TITLE), |
335 default_path.empty() ? *last_opened_path_ : default_path, | 340 default_path.empty() ? *last_opened_path_ : default_path, |
336 parent, true, true, params, | 341 parent, true, true, params, |
337 &SelectFileDialogImplKDE::OnSelectMultiFileDialogResponse); | 342 &SelectFileDialogImplKDE::OnSelectMultiFileDialogResponse); |
338 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, dialog_task); | 343 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, dialog_task); |
339 } | 344 } |
340 | 345 |
341 void SelectFileDialogImplKDE::CreateSaveAsDialog( | 346 void SelectFileDialogImplKDE::CreateSaveAsDialog( |
342 const std::string& title, const FilePath& default_path, | 347 const std::string& title, const FilePath& default_path, |
343 gfx::NativeWindow parent, void* params) { | 348 gfx::NativeWindow parent, void* params) { |
344 std::string title_string = !title.empty() ? title : | |
345 l10n_util::GetStringUTF8(IDS_SAVE_AS_DIALOG_TITLE); | |
346 | |
347 Task* dialog_task = | 349 Task* dialog_task = |
348 NewRunnableMethod( | 350 NewRunnableMethod( |
349 this, &SelectFileDialogImplKDE::CallKDialogOutput, | 351 this, &SelectFileDialogImplKDE::CallKDialogOutput, |
350 std::string("--getsavefilename"), title, | 352 std::string("--getsavefilename"), |
| 353 GetTitle(title, IDS_SAVE_AS_DIALOG_TITLE), |
351 default_path.empty() ? *last_saved_path_ : default_path, | 354 default_path.empty() ? *last_saved_path_ : default_path, |
352 parent, true, false, params, | 355 parent, true, false, params, |
353 &SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse); | 356 &SelectFileDialogImplKDE::OnSelectSingleFileDialogResponse); |
354 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, dialog_task); | 357 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, dialog_task); |
355 } | 358 } |
356 | 359 |
357 void SelectFileDialogImplKDE::SelectSingleFileHelper(const std::string& output, | 360 void SelectFileDialogImplKDE::SelectSingleFileHelper(const std::string& output, |
358 int exit_code, void* params, bool allow_folder) { | 361 int exit_code, void* params, bool allow_folder) { |
359 VLOG(1) << "[kdialog] SingleFileResponse: " << output << std::endl; | 362 VLOG(1) << "[kdialog] SingleFileResponse: " << output << std::endl; |
360 if (exit_code != 0 || output.empty()) { | 363 if (exit_code != 0 || output.empty()) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 continue; | 409 continue; |
407 filenames_fp.push_back(path); | 410 filenames_fp.push_back(path); |
408 } | 411 } |
409 | 412 |
410 if (filenames_fp.empty()) { | 413 if (filenames_fp.empty()) { |
411 FileNotSelected(params); | 414 FileNotSelected(params); |
412 return; | 415 return; |
413 } | 416 } |
414 MultiFilesSelected(filenames_fp, params); | 417 MultiFilesSelected(filenames_fp, params); |
415 } | 418 } |
416 | |
OLD | NEW |