diff options
author | Simon Rettberg | 2019-06-24 15:26:58 +0200 |
---|---|---|
committer | Simon Rettberg | 2019-06-24 15:26:58 +0200 |
commit | 01ee02258797cef715e958ce6d32e7dee0b9831b (patch) | |
tree | f7e58810054bfe9fa098dc9f469ec7cc50285c63 /src | |
parent | Save everything (thanks QTCreator) (diff) | |
download | vmchooser2-01ee02258797cef715e958ce6d32e7dee0b9831b.tar.gz vmchooser2-01ee02258797cef715e958ce6d32e7dee0b9831b.tar.xz vmchooser2-01ee02258797cef715e958ce6d32e7dee0b9831b.zip |
Improve OS icon detection, add Win9x and Win 10
Diffstat (limited to 'src')
-rw-r--r-- | src/images.qrc | 4 | ||||
-rw-r--r-- | src/img/freebsd.png (renamed from src/img/bsd.png) | bin | 7482 -> 7482 bytes | |||
-rw-r--r-- | src/img/win10.png | bin | 0 -> 1421 bytes | |||
-rw-r--r-- | src/img/win9x.png | bin | 0 -> 4110 bytes | |||
-rw-r--r-- | src/vsession.cpp | 81 |
5 files changed, 59 insertions, 26 deletions
diff --git a/src/images.qrc b/src/images.qrc index 938fac5..dda48f1 100644 --- a/src/images.qrc +++ b/src/images.qrc @@ -8,7 +8,7 @@ <file alias="gentoo">img/gentoo.png</file> <file alias="redhat">img/redhat.png</file> <file alias="fedora">img/fedora.png</file> - <file alias="bsd">img/bsd.png</file> + <file alias="freebsd">img/freebsd.png</file> <file alias="osx">img/osx.png</file> <file alias="macos">img/macos.png</file> <file alias="gnome">img/gnome.png</file> @@ -34,8 +34,10 @@ <file alias="opensolaris">img/opensolaris.png</file> <file alias="solaris">img/solaris.png</file> <file alias="win311">img/win311.png</file> + <file alias="win9x">img/win9x.png</file> <file alias="win7">img/win7.png</file> <file alias="win8">img/win8.png</file> + <file alias="win10">img/win10.png</file> <file alias="winxp">img/winxp.png</file> <file alias="win2000">img/win2000.png</file> <file alias="dos">img/msdos.png</file> diff --git a/src/img/bsd.png b/src/img/freebsd.png Binary files differindex ac370ca..ac370ca 100644 --- a/src/img/bsd.png +++ b/src/img/freebsd.png diff --git a/src/img/win10.png b/src/img/win10.png Binary files differnew file mode 100644 index 0000000..3f61325 --- /dev/null +++ b/src/img/win10.png diff --git a/src/img/win9x.png b/src/img/win9x.png Binary files differnew file mode 100644 index 0000000..75f0a56 --- /dev/null +++ b/src/img/win9x.png diff --git a/src/vsession.cpp b/src/vsession.cpp index 92cc9c4..d5cc966 100644 --- a/src/vsession.cpp +++ b/src/vsession.cpp @@ -10,6 +10,8 @@ #include <QIcon> #include <QMessageBox> #include <QHostInfo> // available since Qt 4.7 +#include <QList> +#include <QRegularExpression> #include <unistd.h> // for getuid() #include <sys/types.h> // for getuid(), getpwuid() #include <pwd.h> // for getpwuid() @@ -20,6 +22,45 @@ static QProcess _process; +struct IconMap { + QString icon; + QRegularExpression expr; + IconMap(const QString &icon, const QString ®exp) + : icon(icon), expr(regexp) {} +}; + +// It's first match wins, so put a check for windows95 before windows9 +static const QList<IconMap> iconMapping( +{ + IconMap("dos", "^(ms)?dos"), + IconMap("win311", "^win(dows)?3($|1)"), + IconMap("win9x", "^win(dows)?(95|98|me|nt)"), + IconMap("winxp", "^win(dows)?(xp|2003|2k3)"), + IconMap("win2000", "^win(dows)?(2000|2k)"), // After win2k3! + IconMap("win7", "^win(dows)?7"), + IconMap("win8", "^win(dows)?8"), + IconMap("win10", "^win(dows)?(9|10)"), // After win95! + IconMap("osx", "^darwin"), + IconMap("suse", "^opensuse"), +}); + +// These match via .startsWith and need to exist as a resource with this exact name +static const QStringList directOsNames( +{ + "amiga", "atari", + "beos", + "debian", + "fedora", "freebsd", + "gentoo", + "macos", + "opensolaris", "os2", "osx", + "redhat", "riscos", + "solaris", "suse", + "ubuntu", +}); + +static const QRegularExpression cleanNameRegex("[^a-z0-9]"); + bool VSession::init(const QDomElement& xml) { QDomElement settingsNode = this->doc_.createElement("settings"); this->doc_.appendChild(settingsNode); @@ -49,45 +90,35 @@ QIcon VSession::icon() const { if (icon.startsWith("http://") || icon.startsWith("https://")) { // try to load icon from url QIcon url_icon(iconHolder->getIcon(QUrl(icon))); - if (!url_icon.isNull()) { + if (!url_icon.isNull()) return url_icon; - } } if (!icon.isEmpty()) { + // See if we have a resource with this name, or if it's an absolute path QIcon res_icon(iconHolder->getIcon(icon)); - if (!res_icon.isNull()) { + if (!res_icon.isNull()) return res_icon; - } } // Everything failed, try to guess the OS QString os(getAttribute("os", "param").toLower()); + os = os.replace(cleanNameRegex, QString()); if (!os.isEmpty()) { - QIcon osi = iconHolder->getIcon(os); - if (!osi.isNull()) - return osi; - // These match vmware guestOS keywords mostly, extend for vbox... - if (os == "dos") - return iconHolder->getIcon("dos"); - if (os.startsWith("windows7")) - return iconHolder->getIcon("win7"); - if (os.startsWith("win31")) - return iconHolder->getIcon("win311"); - if (os.startsWith("windows8")) - return iconHolder->getIcon("win8"); - if (os.startsWith("win2000")) - return iconHolder->getIcon("win2000"); - if (os.startsWith("winxp")) - return iconHolder->getIcon("winxp"); - if (os.startsWith("debian")) - return iconHolder->getIcon("debian"); - if (os.startsWith("ubuntu")) - return iconHolder->getIcon("ubuntu"); + // Now try known good OS names via .startsWith() + for (const QString& str : directOsNames) { + if (os.startsWith(str)) + return iconHolder->getIcon(str); + } + // Fuzzy matching via regex + for (const IconMap& map : iconMapping) { + if (map.expr.match(os).hasMatch()) + return iconHolder->getIcon(map.icon); + } + // Running out of ideas... if (os.startsWith("win")) return iconHolder->getIcon("windows"); if (os.contains("linux")) return iconHolder->getIcon("linux"); } - // TODO: Maybe parse title of entry for an OS guess? // Fallback to generic virtualizer icon if (imgtype() == VMWARE) return iconHolder->getIcon("vmware"); |