Home | History | Annotate | Download | only in libgtk2ui
      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.
      6 
      7 #include "chrome/browser/ui/libgtk2ui/select_file_dialog_impl.h"
      8 
      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"
     14 
     15 using content::BrowserThread;
     16 
     17 namespace {
     18 
     19 enum UseKdeFileDialogStatus {
     20   UNKNOWN,
     21   NO_KDE,
     22   YES_KDE
     23 };
     24 
     25 UseKdeFileDialogStatus use_kde_ = UNKNOWN;
     26 
     27 }  // namespace
     28 
     29 namespace libgtk2ui {
     30 
     31 base::FilePath* SelectFileDialogImpl::last_saved_path_ = NULL;
     32 base::FilePath* SelectFileDialogImpl::last_opened_path_ = NULL;
     33 
     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;
     42 
     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;
     54         }
     55       }
     56     }
     57   }
     58 
     59   if (use_kde_ == NO_KDE)
     60     return SelectFileDialogImpl::NewSelectFileDialogImplGTK(listener, policy);
     61 
     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);
     67 }
     68 
     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();
     77   }
     78 }
     79 
     80 SelectFileDialogImpl::~SelectFileDialogImpl() { }
     81 
     82 void SelectFileDialogImpl::ListenerDestroyed() {
     83   listener_ = NULL;
     84 }
     85 
     86 bool SelectFileDialogImpl::CallDirectoryExistsOnUIThread(
     87     const base::FilePath& path) {
     88   base::ThreadRestrictions::ScopedAllowIO allow_io;
     89   return base::DirectoryExists(path);
     90 }
     91 
     92 }  // namespace libgtk2ui
     93