| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "infoplistdockicon.h" |
| 16 | |
| 17 | #include <QCoreApplication> |
| 18 | #include <QDomDocument> |
| 19 | #include <QDomNode> |
| 20 | #include <QFile> |
| 21 | #include <QFileInfo> |
| 22 | #include <QTextStream> |
| 23 | |
| 24 | bool InfoPlistDockIcon::readShowDockIcon() { |
| 25 | const bool show_default = true; |
| 26 | |
| 27 | QString infoPlistPath = |
| 28 | QFileInfo(QCoreApplication::applicationDirPath() + "/../Info.plist") |
| 29 | .canonicalFilePath(); |
| 30 | |
| 31 | QFile plistFile(infoPlistPath); |
| 32 | |
| 33 | QDomDocument doc; |
| 34 | if (!doc.setContent(&plistFile)) return show_default; |
| 35 | |
| 36 | if (!doc.hasChildNodes()) return show_default; |
| 37 | |
| 38 | QDomNodeList nodes = doc.childNodes(); |
| 39 | |
| 40 | QDomNode node; |
| 41 | int i; |
| 42 | for (i = 0; i < nodes.size(); ++i) { |
| 43 | node = nodes.at(i); |
| 44 | if (node.nodeName() == "plist") break; |
| 45 | } |
| 46 | |
| 47 | if (i == nodes.size()) return show_default; |
| 48 | |
| 49 | if (!node.hasChildNodes()) return show_default; |
| 50 | |
| 51 | nodes = node.childNodes(); |
| 52 | |
| 53 | for (i = 0; i < nodes.size(); ++i) { |
| 54 | node = nodes.at(i); |
| 55 | if (node.nodeName() == "dict") break; |
| 56 | } |
| 57 | |
| 58 | if (i == nodes.size()) return show_default; |
| 59 | |
| 60 | if (!node.hasChildNodes()) return show_default; |
| 61 | |
| 62 | nodes = node.childNodes(); |
| 63 | |
| 64 | for (i = 0; i < nodes.size(); ++i) { |
| 65 | node = nodes.at(i); |
| 66 | |
| 67 | QString subText; |
| 68 | |
| 69 | if (node.hasChildNodes()) { |
| 70 | subText = node.childNodes().at(0).toText().data(); |
| 71 | } |
| 72 | |
| 73 | if (subText == "LSUIElement") break; |
| 74 | } |
| 75 | |
| 76 | if (i < nodes.size()) { |
| 77 | node = node.nextSibling(); |
| 78 | return (node.nodeName() != "true"); |
| 79 | } |
| 80 | |
| 81 | return show_default; |
| 82 | } |
| 83 | |
| 84 | void InfoPlistDockIcon::setShowDockIcon(bool show) { |
| 85 | QString infoPlistPath = |
| 86 | QFileInfo(QCoreApplication::applicationDirPath() + "/../Info.plist") |
| 87 | .canonicalFilePath(); |
| 88 | |
| 89 | QFile plistFile(infoPlistPath); |
| 90 | |
| 91 | QDomDocument doc; |
| 92 | if (!doc.setContent(&plistFile) || !doc.hasChildNodes()) return; |
| 93 | |
| 94 | QDomNodeList nodes = doc.childNodes(); |
| 95 | |
| 96 | QDomNode node; |
| 97 | int i; |
| 98 | for (i = 0; i < nodes.size(); ++i) { |
| 99 | node = nodes.at(i); |
| 100 | if (node.nodeName() == "plist") break; |
| 101 | } |
| 102 | |
| 103 | if ((i == nodes.size()) || !node.hasChildNodes()) return; |
| 104 | |
| 105 | nodes = node.childNodes(); |
| 106 | |
| 107 | for (i = 0; i < nodes.size(); ++i) { |
| 108 | node = nodes.at(i); |
| 109 | if (node.nodeName() == "dict") break; |
| 110 | } |
| 111 | |
| 112 | if ((i == nodes.size()) || !node.hasChildNodes()) return; |
| 113 | |
| 114 | nodes = node.childNodes(); |
| 115 | |
| 116 | for (i = 0; i < nodes.size(); ++i) { |
| 117 | node = nodes.at(i); |
| 118 | |
| 119 | QString subText; |
| 120 | |
| 121 | if (node.hasChildNodes()) { |
| 122 | subText = node.childNodes().at(0).toText().data(); |
| 123 | } |
| 124 | |
| 125 | if (subText == "LSUIElement") break; |
| 126 | } |
| 127 | |
| 128 | if (i >= nodes.size()) return; |
| 129 | |
| 130 | node = node.nextSibling(); |
| 131 | |
| 132 | node.toElement().setTagName(show ? "false" : "true"); |
| 133 | |
| 134 | QString modifiedContent = doc.toString(4); |
| 135 | |
| 136 | plistFile.close(); |
| 137 | |
| 138 | if (!plistFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) return; |
| 139 | |
| 140 | QTextStream plistStream(&plistFile); |
| 141 | |
| 142 | plistStream << modifiedContent; |
| 143 | |
| 144 | plistFile.close(); |
| 145 | } |