Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / libgtk2ui / select_file_dialog_impl.cc
blob39bea0b072746d0c75fc9a4857a5418037cad03d
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file implements common select dialog functionality between GTK and KDE.
7 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h"
9 #include "base/environment.h"
10 #include "base/files/file_util.h"
11 #include "base/nix/xdg_util.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "content/public/browser/browser_thread.h"
15 using content::BrowserThread;
17 namespace {
19 enum UseKdeFileDialogStatus {
20 UNKNOWN,
21 NO_KDE,
22 YES_KDE
25 UseKdeFileDialogStatus use_kde_ = UNKNOWN;
27 } // namespace
29 namespace libgtk2ui {
31 base::FilePath* SelectFileDialogImpl::last_saved_path_ = NULL;
32 base::FilePath* SelectFileDialogImpl::last_opened_path_ = NULL;
34 // static
35 ui::SelectFileDialog* SelectFileDialogImpl::Create(
36 ui::SelectFileDialog::Listener* listener,
37 ui::SelectFilePolicy* policy) {
38 DCHECK_CURRENTLY_ON(BrowserThread::UI);
39 if (use_kde_ == UNKNOWN) {
40 // Start out assumimg we are not going to use KDE.
41 use_kde_ = NO_KDE;
43 // Check to see if KDE is the desktop environment.
44 scoped_ptr<base::Environment> env(base::Environment::Create());
45 base::nix::DesktopEnvironment desktop =
46 base::nix::GetDesktopEnvironment(env.get());
47 if (desktop == base::nix::DESKTOP_ENVIRONMENT_KDE3 ||
48 desktop == base::nix::DESKTOP_ENVIRONMENT_KDE4) {
49 // Check to see if the user dislikes the KDE file dialog.
50 if (!env->HasVar("NO_CHROME_KDE_FILE_DIALOG")) {
51 // Check to see if the KDE dialog works.
52 if (SelectFileDialogImpl::CheckKDEDialogWorksOnUIThread()) {
53 use_kde_ = YES_KDE;
59 if (use_kde_ == NO_KDE)
60 return SelectFileDialogImpl::NewSelectFileDialogImplGTK(listener, policy);
62 scoped_ptr<base::Environment> env(base::Environment::Create());
63 base::nix::DesktopEnvironment desktop =
64 base::nix::GetDesktopEnvironment(env.get());
65 return SelectFileDialogImpl::NewSelectFileDialogImplKDE(
66 listener, policy, desktop);
69 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener,
70 ui::SelectFilePolicy* policy)
71 : SelectFileDialog(listener, policy),
72 file_type_index_(0),
73 type_(SELECT_NONE) {
74 if (!last_saved_path_) {
75 last_saved_path_ = new base::FilePath();
76 last_opened_path_ = new base::FilePath();
80 SelectFileDialogImpl::~SelectFileDialogImpl() { }
82 void SelectFileDialogImpl::ListenerDestroyed() {
83 listener_ = NULL;
86 bool SelectFileDialogImpl::CallDirectoryExistsOnUIThread(
87 const base::FilePath& path) {
88 base::ThreadRestrictions::ScopedAllowIO allow_io;
89 return base::DirectoryExists(path);
92 } // namespace libgtk2ui