QVariant ValueGetters::MagnetAssociationValueGetter(QString group, QString name, QVariant defaultValue) { #ifdef Q_WS_WIN QSettings assocSettings("HKEY_CURRENT_USER\\SOFTWARE\\Classes", QSettings::NativeFormat); QString magnetCommand = QDir::toNativeSeparators(assocSettings.value("Magnet/shell/open/command/.").toString()); QString applicationFilePath = QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).absoluteFilePath()); return QVariant::fromValue(magnetCommand.contains(applicationFilePath, Qt::CaseInsensitive) == true); #else Q_WS_X11 QFile associtaionGnomeConfig(StaticHelpers::CombinePathes(QDir::homePath() , ".config/mimeapps.list")); if (associtaionGnomeConfig.open(QFile::ReadOnly)) { QByteArray asscoiationData = associtaionGnomeConfig.readAll(); associtaionGnomeConfig.close(); QString associationStr = QString::fromUtf8(asscoiationData); QStringList lines = associationStr.split('\n'); foreach(QString line, lines) { if (line.startsWith("x-scheme-handler/magnet", Qt::CaseInsensitive)) { return QVariant::fromValue(line.endsWith("CuteTorrent.desktop;", Qt::CaseInsensitive) == true); } } associtaionGnomeConfig.close(); }
void ValueSetters::MagnetAssociationValueSetter(QString group, QString name, QVariant value) { bool associate = value.toBool(); #ifdef Q_WS_WIN QSettings asocSettings("HKEY_CLASSES_ROOT", QSettings::NativeFormat); QString applicationFilePath = QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).absoluteFilePath()); if (associate) { asocSettings.setValue("Magnet/.", "Magnet URI"); asocSettings.setValue("Magnet/Content Type", "application/x-magnet"); asocSettings.setValue("Magnet/URL Protocol", ""); asocSettings.setValue("Magnet/shell/open/command/.", "\"" + applicationFilePath + "\"" + " \"%1\""); } else { asocSettings.remove("Magnet/."); asocSettings.remove("Magnet/Content Type"); asocSettings.remove("Magnet/URL Protocol"); asocSettings.remove("Magnet/shell/open/command/."); } #elif Q_WS_X11 QFile associtaionGnomeConfig(StaticHelpers::CombinePathes(QDir::homePath() , ".config/mimeapps.list")); if (associtaionGnomeConfig.open(QFile::ReadOnly)) { QByteArray asscoiationData = associtaionGnomeConfig.readAll(); associtaionGnomeConfig.close(); QString associationStr = QString::fromUtf8(asscoiationData); QStringList lines = associationStr.split('\n'); bool magnetFound = false; for (int i = 0; i < lines.size(); i++) { QString line = lines[i]; if (line.startsWith("x-scheme-handler/magnet", Qt::CaseInsensitive)) { magnetFound = true; if (associate) { lines[i] = "x-scheme-handler/magnet=CuteTorrent.desktop;"; } else { lines[i] = ""; } } } if (!magnetFound && associate) { lines.append("x-scheme-handler/magnet=CuteTorrent.desktop;\n"); } if (associtaionGnomeConfig.open(QFile::WriteOnly)) { for (int i = 0; i < lines.size(); i++) { QString line = lines[i]; if (!line.isEmpty()) { associtaionGnomeConfig.write((line + "\n").toUtf8()); } } associtaionGnomeConfig.close(); } else { qCritical() << "Unable to open gnome config file for writing file asscoiations" << associtaionGnomeConfig.errorString(); } } else { qCritical() << "Unable to open gnome config file for reading file asscoiations" << associtaionGnomeConfig.errorString(); } #endif }