[tor-commits] [vidalia/master] Implement Icon Preference (appearance setting) in OSX (see #2163)

chiiph at torproject.org chiiph at torproject.org
Fri Oct 7 19:20:39 UTC 2011


commit 4df4ee18c1acbc9fdad4e2395a8384190ff420db
Author: Jason Klein <trac.torproject.org at my.jrklein.com>
Date:   Sun Apr 17 17:13:25 2011 -0500

    Implement Icon Preference (appearance setting) in OSX (see #2163)
    
    Updated the Appearance settings page to include an Icon Preference
    group box with three new radio options:
      Show Tray Icon and Dock Icon
      Hide the Dock Icon
      Hide the Tray Icon
    
    In order to hide the Dock Icon, I had to create a custom Info.plist
    file with LSUIElement set to "1" so OSX would launch Vidalia in the
    background (without Dock Icon).  I then use TransformProcessType to
    force Vidalia to the foreground (show Dock Icon).
    
    Hiding the Tray Icon was trival. I only call "_trayIcon.setIcon"
    if the user wants to see the System/Tray Icon.
    
    By default, the user will continue to see Tray Icon and Dock Icon.
---
 pkg/osx/MacOSXBundleInfo.plist.in      |   38 ++++++++++++++++++++++
 src/vidalia/CMakeLists.txt             |    3 ++
 src/vidalia/MainWindow.cpp             |   22 ++++++++++++-
 src/vidalia/config/AppearancePage.cpp  |   28 ++++++++++++++++
 src/vidalia/config/AppearancePage.ui   |   55 ++++++++++++++++++++++++++++++++
 src/vidalia/config/VidaliaSettings.cpp |   17 ++++++++++
 src/vidalia/config/VidaliaSettings.h   |    6 +++
 7 files changed, 168 insertions(+), 1 deletions(-)

diff --git a/pkg/osx/MacOSXBundleInfo.plist.in b/pkg/osx/MacOSXBundleInfo.plist.in
new file mode 100644
index 0000000..3b5bfe0
--- /dev/null
+++ b/pkg/osx/MacOSXBundleInfo.plist.in
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>CFBundleDevelopmentRegion</key>
+	<string>English</string>
+	<key>CFBundleExecutable</key>
+	<string>${MACOSX_BUNDLE_EXECUTABLE_NAME}</string>
+	<key>CFBundleGetInfoString</key>
+	<string>${MACOSX_BUNDLE_INFO_STRING}</string>
+	<key>CFBundleIconFile</key>
+	<string>${MACOSX_BUNDLE_ICON_FILE}</string>
+	<key>CFBundleIdentifier</key>
+	<string>${MACOSX_BUNDLE_GUI_IDENTIFIER}</string>
+	<key>CFBundleInfoDictionaryVersion</key>
+	<string>6.0</string>
+	<key>CFBundleLongVersionString</key>
+	<string>${MACOSX_BUNDLE_LONG_VERSION_STRING}</string>
+	<key>CFBundleName</key>
+	<string>${MACOSX_BUNDLE_BUNDLE_NAME}</string>
+	<key>CFBundlePackageType</key>
+	<string>APPL</string>
+	<key>CFBundleShortVersionString</key>
+	<string>${MACOSX_BUNDLE_SHORT_VERSION_STRING}</string>
+	<key>CFBundleSignature</key>
+	<string>????</string>
+	<key>CFBundleVersion</key>
+	<string>${MACOSX_BUNDLE_BUNDLE_VERSION}</string>
+	<key>CSResourcesFileMapped</key>
+	<true/>
+	<key>LSRequiresCarbon</key>
+	<true/>
+	<key>NSHumanReadableCopyright</key>
+	<string>${MACOSX_BUNDLE_COPYRIGHT}</string>
+        <key>LSUIElement</key>
+        <string>1</string>
+</dict>
+</plist>
diff --git a/src/vidalia/CMakeLists.txt b/src/vidalia/CMakeLists.txt
index 08fb510..be3e012 100644
--- a/src/vidalia/CMakeLists.txt
+++ b/src/vidalia/CMakeLists.txt
@@ -361,6 +361,9 @@ if (APPLE)
   set(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/)
   add_executable(${vidalia_BIN} MACOSX_BUNDLE ${vidalia_SRCS})
 
+  ## Specify location of custom Info.plist file
+  set_target_properties(${vidalia_BIN} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ../pkg/osx/MacOSXBundleInfo.plist.in)
+
   ## Get the location of the app bundle for the current configuration
   get_target_property(VIDALIA_EXECUTABLE ${vidalia_BIN} LOCATION)
   get_filename_component(MACOSX_BUNDLE_DIRECTORY ${VIDALIA_EXECUTABLE} PATH)
diff --git a/src/vidalia/MainWindow.cpp b/src/vidalia/MainWindow.cpp
index b159d2e..967e2df 100644
--- a/src/vidalia/MainWindow.cpp
+++ b/src/vidalia/MainWindow.cpp
@@ -39,6 +39,10 @@
 #include <QTimer>
 #include <QTextStream>
 
+#ifdef Q_WS_MAC
+#include <Carbon/Carbon.h>
+#endif
+
 #define IMG_BWGRAPH        ":/images/16x16/utilities-system-monitor.png"
 #define IMG_CONTROL_PANEL  ":/images/16x16/system-run.png"
 #define IMG_MESSAGELOG     ":/images/16x16/format-justify-fill.png"
@@ -215,6 +219,16 @@ MainWindow::MainWindow()
     show(); 
   /* Optimistically hope that the tray icon gets added. */
   _trayIcon.show();
+
+#if defined(Q_WS_MAC)
+  /* Display OSX dock icon if icon preference is not set to "Tray Only" */
+  if (settings.getIconPref() != "Tray") {
+      ProcessSerialNumber psn = { 0, kCurrentProcess };
+      TransformProcessType(&psn, kProcessTransformToForegroundApplication);
+  }
+  /* Vidalia launched in background (LSUIElement=true). Bring to foreground. */
+  VidaliaWindow::setVisible(true);
+#endif
 }
 
 /** Destructor. */
@@ -572,9 +586,15 @@ void
 MainWindow::setTrayIcon(const QString &iconFile)
 {
 #if defined(Q_WS_MAC)
+  VidaliaSettings settings;
   QApplication::setWindowIcon(QPixmap(iconFile));
-#endif
+  /* only display tray icon if icon preference is not set to "Dock Only" */
+  if (settings.getIconPref() != "Dock")
+    _trayIcon.setIcon(QIcon(iconFile));
+#else
+  /* always display tray icon for other platforms */
   _trayIcon.setIcon(QIcon(iconFile));
+#endif
 }
 
 /** Respond to a double-click on the tray icon by opening the Control Panel
diff --git a/src/vidalia/config/AppearancePage.cpp b/src/vidalia/config/AppearancePage.cpp
index 11939c3..647b67a 100644
--- a/src/vidalia/config/AppearancePage.cpp
+++ b/src/vidalia/config/AppearancePage.cpp
@@ -72,6 +72,21 @@ AppearancePage::save(QString &errmsg)
   /* Set the new style */
   Vidalia::setStyle(ui.cmboStyle->currentText());
   _settings->setInterfaceStyle(ui.cmboStyle->currentText());
+
+#if defined(Q_WS_MAC)
+  /* Save new icon preference */
+  if(ui.rdoIconPrefDock->isChecked()) {
+    _settings->setIconPref("Dock");
+  }
+  else if(ui.rdoIconPrefTray->isChecked()) {
+    _settings->setIconPref("Tray");
+  }
+  else {
+    /* default setting */
+    _settings->setIconPref("Both");
+  }
+#endif
+
   return true;
 }
   
@@ -84,5 +99,18 @@ AppearancePage::load()
   
   index = ui.cmboStyle->findData(Vidalia::style().toLower());
   ui.cmboStyle->setCurrentIndex(index);
+
+#if defined(Q_WS_MAC)
+    /* set current icon preference */
+    ui.rdoIconPrefBoth->setChecked(_settings->getIconPref() == "Both");
+    ui.rdoIconPrefTray->setChecked(_settings->getIconPref() == "Tray");
+    ui.rdoIconPrefDock->setChecked(_settings->getIconPref() == "Dock");
+#else
+    /* hide preference on non-OSX platforms */
+    ui.grpIconPref->setVisible(false);
+    ui.rdoIconPrefBoth->setVisible(false);
+    ui.rdoIconPrefTray->setVisible(false);
+    ui.rdoIconPrefDock->setVisible(false);
+#endif
 }
 
diff --git a/src/vidalia/config/AppearancePage.ui b/src/vidalia/config/AppearancePage.ui
index 8672af3..ef7ef5f 100644
--- a/src/vidalia/config/AppearancePage.ui
+++ b/src/vidalia/config/AppearancePage.ui
@@ -111,6 +111,61 @@
     </widget>
    </item>
    <item>
+    <widget class="QGroupBox" name="grpIconPref">
+     <property name="minimumSize">
+      <size>
+       <width>0</width>
+       <height>100</height>
+      </size>
+     </property>
+     <property name="contextMenuPolicy">
+      <enum>Qt::PreventContextMenu</enum>
+     </property>
+     <property name="title">
+      <string>System Icon Preferences (changes will take effect when you restart Vidalia)</string>
+     </property>
+     <widget class="QRadioButton" name="rdoIconPrefBoth">
+      <property name="geometry">
+       <rect>
+        <x>20</x>
+        <y>30</y>
+        <width>441</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Show the Tray Icon and Dock Icon (default)</string>
+      </property>
+     </widget>
+     <widget class="QRadioButton" name="rdoIconPrefDock">
+      <property name="geometry">
+       <rect>
+        <x>20</x>
+        <y>70</y>
+        <width>441</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Hide the Tray Icon</string>
+      </property>
+     </widget>
+     <widget class="QRadioButton" name="rdoIconPrefTray">
+      <property name="geometry">
+       <rect>
+        <x>20</x>
+        <y>50</y>
+        <width>441</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Hide the Dock Icon</string>
+      </property>
+     </widget>
+    </widget>
+   </item>
+   <item>
     <spacer>
      <property name="orientation" >
       <enum>Qt::Vertical</enum>
diff --git a/src/vidalia/config/VidaliaSettings.cpp b/src/vidalia/config/VidaliaSettings.cpp
index 0812b03..858ca8e 100644
--- a/src/vidalia/config/VidaliaSettings.cpp
+++ b/src/vidalia/config/VidaliaSettings.cpp
@@ -45,6 +45,9 @@
 #define VIDALIA_REG_KEY        "Vidalia" 
 #endif
 
+#if defined(Q_WS_MAC)
+#define SETTING_ICON_PREF	"IconPref"
+#endif
 
 /** Default Constructor */
 VidaliaSettings::VidaliaSettings()
@@ -82,6 +85,7 @@ VidaliaSettings::VidaliaSettings()
   setDefault(SETTING_LAST_UPDATE_CHECK, QDateTime());
   setDefault(SETTING_USE_LOCAL_GEOIP_DATABASE, false);
   setDefault(SETTING_LOCAL_GEOIP_DATABASE, "");
+  setDefault(SETTING_ICON_PREF, "Both");
 }
 
 /** Gets the currently preferred language code for Vidalia. */
@@ -321,3 +325,16 @@ VidaliaSettings::setLocalGeoIpDatabase(const QString &databaseFile)
   setValue(SETTING_LOCAL_GEOIP_DATABASE, databaseFile);
 }
 
+/** Get the icon preference */
+QString
+VidaliaSettings::getIconPref() const
+{
+  return value(SETTING_ICON_PREF).toString();
+}
+
+/** Set the icon preference */
+void
+VidaliaSettings::setIconPref(const QString &iconPref)
+{
+  setValue(SETTING_ICON_PREF, iconPref);
+}
diff --git a/src/vidalia/config/VidaliaSettings.h b/src/vidalia/config/VidaliaSettings.h
index 2667b12..2efec12 100644
--- a/src/vidalia/config/VidaliaSettings.h
+++ b/src/vidalia/config/VidaliaSettings.h
@@ -125,6 +125,12 @@ public:
   QString localGeoIpDatabase() const;
   /** Sets the file to use as a local GeoIP database. */
   void setLocalGeoIpDatabase(const QString &databaseFile);
+
+  /** Get the icon preference */
+  QString getIconPref() const;
+
+  /** Set the icon preference */
+  void setIconPref(const QString &iconPref);
 };
 
 #endif





More information about the tor-commits mailing list