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#include "chrome/browser/printing/printer_manager_dialog.h"
6
7#include "base/bind.h"
8#include "base/environment.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/message_loop/message_loop.h"
11#include "base/nix/xdg_util.h"
12#include "base/process/kill.h"
13#include "base/process/launch.h"
14#include "content/public/browser/browser_thread.h"
15
16using base::Environment;
17using content::BrowserThread;
18
19namespace {
20
21// KDE printer config command ("system-config-printer-kde") causes the
22// OptionWidget to crash (https://bugs.kde.org/show_bug.cgi?id=271957).
23// Therefore, use GNOME printer config command for KDE.
24const char kGNOMEPrinterConfigCommand[] = "system-config-printer";
25
26// Detect the command based on the deskop environment and open the printer
27// manager dialog.
28void DetectAndOpenPrinterConfigDialog() {
29  DCHECK_CURRENTLY_ON(BrowserThread::FILE);
30  scoped_ptr<Environment> env(Environment::Create());
31
32  const char* command = NULL;
33  switch (base::nix::GetDesktopEnvironment(env.get())) {
34    case base::nix::DESKTOP_ENVIRONMENT_GNOME:
35    case base::nix::DESKTOP_ENVIRONMENT_KDE3:
36    case base::nix::DESKTOP_ENVIRONMENT_KDE4:
37    case base::nix::DESKTOP_ENVIRONMENT_UNITY:
38      command = kGNOMEPrinterConfigCommand;
39      break;
40    case base::nix::DESKTOP_ENVIRONMENT_XFCE:
41    case base::nix::DESKTOP_ENVIRONMENT_OTHER:
42      break;
43  }
44
45  if (!command) {
46    LOG(ERROR) << "Failed to detect the command to open printer config dialog";
47    return;
48  }
49
50  std::vector<std::string> argv;
51  argv.push_back(command);
52  base::ProcessHandle handle;
53  if (!base::LaunchProcess(argv, base::LaunchOptions(), &handle)) {
54    LOG(ERROR) << "Failed to open printer manager dialog ";
55    return;
56  }
57  base::EnsureProcessGetsReaped(handle);
58}
59
60}  // anonymous namespace
61
62namespace printing {
63
64void PrinterManagerDialog::ShowPrinterManagerDialog() {
65  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
66                          base::Bind(&DetectAndOpenPrinterConfigDialog));
67}
68
69}  // namespace printing
70