]> git.saurik.com Git - wxWidgets.git/commitdiff
add wxGetLinuxDistributionInfo() and wxPlatformInfo::GetLinuxDistribution() functions...
authorFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Tue, 2 Jun 2009 13:01:41 +0000 (13:01 +0000)
committerFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Tue, 2 Jun 2009 13:01:41 +0000 (13:01 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60873 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/platinfo.h
include/wx/utils.h
interface/wx/platinfo.h
interface/wx/utils.h
src/common/platinfo.cpp
src/unix/utilsunx.cpp
tests/font/fonttest.cpp

index 2a4e8621cfaa7d4160d0f3a9c6dc83e64869695b..cb2de2f3fdc0f777946a93a163fe319dd2d4f79e 100644 (file)
@@ -15,7 +15,7 @@
 #include "wx/string.h"
 
 // ----------------------------------------------------------------------------
-// wxPlatformInfo
+// wxPlatformInfo enums & structs
 // ----------------------------------------------------------------------------
 
 // VERY IMPORTANT: when changing these enum values, also change the relative
@@ -114,6 +114,31 @@ enum wxEndianness
     wxENDIAN_MAX
 };
 
+// informations about a linux distro returned by the lsb_release utility
+struct wxLinuxDistributionInfo
+{
+    wxString Id;
+    wxString Release;
+    wxString CodeName;
+    wxString Description;
+    
+    bool operator==(const wxLinuxDistributionInfo& ldi) const
+    { 
+        return Id == ldi.Id &&
+               Release == ldi.Release &&
+               CodeName == ldi.CodeName &&
+               Description == ldi.Description;
+    }
+    
+    bool operator!=(const wxLinuxDistributionInfo& ldi) const
+    { return !(*this == ldi); }
+};
+
+
+// ----------------------------------------------------------------------------
+// wxPlatformInfo
+// ----------------------------------------------------------------------------
+
 // Information about the toolkit that the app is running under and some basic
 // platform and architecture info
 class WXDLLIMPEXP_BASE wxPlatformInfo
@@ -161,6 +186,7 @@ public:
     static wxString GetArchName(wxArchitecture arch);
     static wxString GetEndiannessName(wxEndianness end);
 
+    
     // getters
     // -----------------
 
@@ -196,6 +222,8 @@ public:
 
     wxOperatingSystemId GetOperatingSystemId() const
         { return m_os; }
+    wxLinuxDistributionInfo GetLinuxDistributionInfo() const
+        { return m_ldi; }
     wxPortId GetPortId() const
         { return m_port; }
     wxArchitecture GetArchitecture() const
@@ -219,6 +247,16 @@ public:
         { return GetArchName(m_arch); }
     wxString GetEndiannessName() const
         { return GetEndiannessName(m_endian); }
+    wxString GetOperatingSystemDescription() const
+        { return m_osDesc; }
+    wxString GetDesktopEnvironment() const
+        { return m_desktopEnv; }
+        
+    static wxString GetOperatingSystemDirectory();
+        // doesn't make sense to store inside wxPlatformInfo the OS directory,
+        // thus this function is static; note that this function simply calls
+        // wxGetOSDirectory() and is here just to make it easier for the user to 
+        // find it that feature (global functions can be difficult to find in the docs)
 
     // setters
     // -----------------
@@ -230,12 +268,20 @@ public:
 
     void SetOperatingSystemId(wxOperatingSystemId n)
         { m_os = n; }
+    void SetOperatingSystemDescription(const wxString& desc)
+        { m_osDesc = desc; }
     void SetPortId(wxPortId n)
         { m_port = n; }
     void SetArchitecture(wxArchitecture n)
         { m_arch = n; }
     void SetEndianness(wxEndianness n)
         { m_endian = n; }
+        
+    void SetDesktopEnvironment(const wxString& de)
+        { m_desktopEnv = de; }
+    void SetLinuxDistributionInfo(const wxLinuxDistributionInfo& di)
+        { m_ldi = di; }
+
 
     // miscellaneous
     // -----------------
@@ -244,9 +290,13 @@ public:
     {
         return m_osVersionMajor != -1 && m_osVersionMinor != -1 &&
                m_os != wxOS_UNKNOWN &&
+               !m_osDesc.IsEmpty() &&
                m_tkVersionMajor != -1 && m_tkVersionMinor != -1 &&
                m_port != wxPORT_UNKNOWN &&
-               m_arch != wxARCH_INVALID && m_endian != wxENDIAN_INVALID;
+               m_arch != wxARCH_INVALID && 
+               m_endian != wxENDIAN_INVALID;
+               
+               // do not check linux-specific info; it's ok to have them empty
     }
 
 
@@ -269,6 +319,16 @@ protected:
 
     // Operating system ID.
     wxOperatingSystemId m_os;
+    
+    // Operating system description.
+    wxString m_osDesc;
+    
+    
+    // linux-specific
+    // -----------------
+    
+    wxString m_desktopEnv;
+    wxLinuxDistributionInfo m_ldi;
 
 
     // toolkit
@@ -288,7 +348,7 @@ protected:
     // others
     // -----------------
 
-    // architecture of the OS
+    // architecture of the OS/machine
     wxArchitecture m_arch;
 
     // endianness of the machine
index 37927b4e1e588b924b26ecdd28ac5706fb0d2da4..dbd88a6342a4145ff3ee38b03dd875d9c6e3ffd0 100644 (file)
@@ -32,7 +32,7 @@ class WXDLLIMPEXP_FWD_BASE wxArrayInt;
 // wxLongLong
 #include "wx/longlong.h"
 
-// need for wxOperatingSystemId
+// needed for wxOperatingSystemId, wxLinuxDistributionInfo
 #include "wx/platinfo.h"
 
 #ifdef __WATCOMC__
@@ -116,6 +116,11 @@ WXDLLIMPEXP_BASE bool wxIsPlatformLittleEndian();
 // Get platform architecture
 WXDLLIMPEXP_BASE bool wxIsPlatform64Bit();
 
+#ifdef __LINUX__
+// Get linux-distro informations
+WXDLLIMPEXP_BASE wxLinuxDistributionInfo wxGetLinuxDistributionInfo();
+#endif
+
 // Return a string with the current date/time
 WXDLLIMPEXP_BASE wxString wxNow();
 
index 88b6a68d8653740db7da5729b4a87b27607b8cbc..b32f810f0cce7f27a10d13e38baf022cbd7787a4 100644 (file)
@@ -107,6 +107,24 @@ enum wxEndianness
     wxENDIAN_MAX
 };
 
+/**
+    A structure containing informations about a Linux distribution as returned 
+    by the @c lsb_release utility.
+    
+    See wxGetLinuxDistributionInfo() or wxPlatformInfo::GetLinuxDistributionInfo()
+    for more info.
+*/
+struct wxLinuxDistributionInfo
+{
+    wxString Id;                //!< The id of the distribution; e.g. "Ubuntu"
+    wxString Release;           //!< The version of the distribution; e.g. "9.04"
+    wxString CodeName;          //!< The code name of the distribution; e.g. "jaunty"
+    wxString Description;       //!< The description of the distribution; e.g. "Ubuntu 9.04"
+    
+    bool operator==(const wxLinuxDistributionInfo& ldi) const;
+    bool operator!=(const wxLinuxDistributionInfo& ldi) const;
+};
+
 
 /**
     @class wxPlatformInfo
@@ -117,8 +135,16 @@ enum wxEndianness
     This class does not only have @e getters for the informations above, it also has
     @e setters. This allows you to e.g. save the current platform informations in a 
     data file (maybe in string form) so that when you later load it, you can easily
-    retrieve (see the static getters for string->enum conversion functions) the 
-    signature of the system which generated it.
+    retrieve (see the static getters for string->enum conversion functions) and store
+    inside a wxPlatformInfo instance (using its setters) the signature of the system 
+    which generated it.
+    
+    In general however you only need to use the static Get() function and then
+    access the various informations for the current platform:
+    @code
+        wxLogMessage("This application is running under %s.",
+                     wxPlatformInfo::Get().GetOperatingSystemIdName());
+    @endcode
 
     @library{wxbase}
     @category{cfg}
@@ -285,6 +311,13 @@ public:
     static wxString GetPortIdShortName(wxPortId port,
                                        bool usingUniversal);
 
+    /**
+        Returns the operating system directory.
+        
+        See wxGetOSDirectory() for more info.
+    */
+    static wxString GetOperatingSystemDirectory();
+
     //@}
     
     
@@ -323,11 +356,32 @@ public:
         Returns the operating system ID of this wxPlatformInfo instance.
     */
     wxOperatingSystemId GetOperatingSystemId() const;
+    
+    /**
+        Returns the description of the operating system of this wxPlatformInfo instance.
+        
+        See wxGetOSDescription() for more info.
+    */
+    wxString GetOperatingSystemDescription() const;
 
     /**
         Returns the wxWidgets port ID associated with this wxPlatformInfo instance.
     */
     wxPortId GetPortId() const;
+    
+    /**
+        Returns the Linux distribution info associated with this wxPlatformInfo instance.
+        
+        See wxGetLinuxDistributionInfo() for more info.
+    */
+    wxLinuxDistributionInfo GetLinuxDistributionInfo() const;
+    
+    /**
+        Returns the desktop environment associated with this wxPlatformInfo instance.
+        
+        See wxAppTraits::GetDesktopEnvironment() for more info.
+    */
+    wxString GetDesktopEnvironment() const;
 
     /**
         Returns the run-time major version of the toolkit associated with this
@@ -436,8 +490,22 @@ public:
         Sets the version of the toolkit associated with this wxPlatformInfo instance.
     */
     void SetToolkitVersion(int major, int minor);
+
+    /**
+        Sets the operating system description associated with this wxPlatformInfo instance.
+    */
+    void SetOperatingSystemDescription(const wxString& desc);
+    /**
+        Sets the desktop environment associated with this wxPlatformInfo instance.
+    */
+    void SetDesktopEnvironment(const wxString& de);
     
-    //@}
+    /**
+        Sets the linux distribution info associated with this wxPlatformInfo instance.
+    */
+    void SetLinuxDistributionInfo(const wxLinuxDistributionInfo& di);
     
+    //@}
 };
 
index d8a7bdd01c78116ae1bc061cfa9e31080fe4bb2e..08b6a55037bdb8a8cd0357b6499b8d42d0097d50 100644 (file)
@@ -684,6 +684,23 @@ bool wxIsPlatform64Bit();
 */
 bool wxIsPlatformLittleEndian();
 
+/**
+    Returns a structure containing informations about the currently running
+    Linux distribution.
+    
+    This function uses the @c lsb_release utility which is part of the 
+    <tt>Linux Standard Base Core</tt> specification 
+    (see http://refspecs.linux-foundation.org/lsb.shtml) since the very first LSB 
+    release 1.0 (released in 2001).
+    The @c lsb_release utility is very common on modern Linux distributions but in
+    case it's not available, then this function will return a ::wxLinuxDistributionInfo
+    structure containing empty strings.
+    
+    This function is Linux-specific and is only available when the @c __LINUX__
+    symbol is defined.
+*/
+wxLinuxDistributionInfo wxGetLinuxDistributionInfo();
+
 //@}
 
 
index fbb27bfd942731bad3ba04ed2c42986d60ac0996..a687a47862f4f96f45282ed860bf952d8ad5edc6 100644 (file)
@@ -105,8 +105,8 @@ static const wxChar* const wxEndiannessNames[] =
 // local functions
 // ----------------------------------------------------------------------------
 
-// returns log in base 2 of the value, this maps the enum values to the
-// corresponding indices
+// returns the logarithm in base 2 of 'value'; this maps the enum values to the
+// corresponding indexes of the string arrays above
 static unsigned wxGetIndexFromEnumValue(int value)
 {
     wxCHECK_MSG( value, (unsigned)-1, _T("invalid enum value") );
@@ -159,6 +159,9 @@ bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const
            m_osVersionMajor == t.m_osVersionMajor &&
            m_osVersionMinor == t.m_osVersionMinor &&
            m_os == t.m_os &&
+           m_osDesc == t.m_osDesc &&
+           m_ldi == t.m_ldi &&
+           m_desktopEnv == t.m_desktopEnv &&
            m_port == t.m_port &&
            m_usingUniversal == t.m_usingUniversal &&
            m_arch == t.m_arch &&
@@ -182,11 +185,18 @@ void wxPlatformInfo::InitForCurrentPlatform()
     {
         m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor);
         m_usingUniversal = traits->IsUsingUniversalWidgets();
+        m_desktopEnv = traits->GetDesktopEnvironment();
     }
 
     m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor);
+    m_osDesc = wxGetOsDescription();
     m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG;
     m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32;
+    
+#ifdef __LINUX__
+    m_ldi = wxGetLinuxDistributionInfo();
+#endif
+    // else: leave m_ldi empty
 }
 
 /* static */
@@ -202,6 +212,12 @@ const wxPlatformInfo& wxPlatformInfo::Get()
     return gs_platInfo;
 }
 
+/* static */
+wxString wxPlatformInfo::GetOperatingSystemDirectory()
+{
+    return wxGetOSDirectory();
+}
+
 
 
 // ----------------------------------------------------------------------------
index 1c92ba898215e8b00be623a139ffcf1a01ab2060..3153216e62c6efe1540a93855a80b712e3bf8701 100644 (file)
@@ -873,6 +873,25 @@ bool wxIsPlatform64Bit()
                 machine.Contains(wxT("alpha"));
 }
 
+#ifdef __LINUX__
+wxLinuxDistributionInfo wxGetLinuxDistributionInfo()
+{
+    const wxString id = wxGetCommandOutput(wxT("lsb_release --id"));
+    const wxString desc = wxGetCommandOutput(wxT("lsb_release --description"));
+    const wxString rel = wxGetCommandOutput(wxT("lsb_release --release"));
+    const wxString codename = wxGetCommandOutput(wxT("lsb_release --codename"));
+    
+    wxLinuxDistributionInfo ret;
+    
+    id.StartsWith("Distributor ID:\t", &ret.Id);
+    desc.StartsWith("Description:\t", &ret.Description);
+    rel.StartsWith("Release:\t", &ret.Release);
+    codename.StartsWith("Codename:\t", &ret.CodeName);
+
+    return ret;
+}
+#endif
+
 // these functions are in src/osx/utilsexc_base.cpp for wxMac
 #ifndef __WXMAC__
 
index 7ba623bd55706cce0a1f87633b3784711f3b55e1..69df21a2f549a07ca914a4c4f2de13804669180d 100644 (file)
@@ -113,6 +113,18 @@ void FontTestCase::GetSet()
         // consider adding another branch to this #if
 #if defined(__WXMSW__) || defined(__WXOSX__)
         static const char *knownGoodFaceName = "Arial";
+#elif defined(__LINUX__)
+        static const char *knownGoodFaceName;
+        wxString distroname = wxGetLinuxDistributionInfo().Id;
+        
+        if (distroname.Contains("Ubuntu"))
+            knownGoodFaceName = "FreeSerif";
+                // ttf-freefont and ttf-dejavu packages are installed by default on [X,K]Ubuntu systems
+        else if (distroname == "Debian")
+            knownGoodFaceName = "Fixed";
+        else
+            knownGoodFaceName = "DejaVu Sans";
+                // this is very popular in many linux distro...
 #else
         static const char *knownGoodFaceName = "Fixed";
 #endif