File indexing completed on 2021-12-21 13:15:45

0001 /***************************************************************************
0002 *   Copyright 2007 Niko Sams <niko.sams@gmail.com>                        *
0003 *                                                                         *
0004 *   This program is free software; you can redistribute it and/or modify  *
0005 *   it under the terms of the GNU General Public License as published by  *
0006 *   the Free Software Foundation; either version 2 of the License, or     *
0007 *   (at your option) any later version.                                   *
0008 *                                                                         *
0009 ***************************************************************************/
0010 #include "uploadprofilemodel.h"
0011 
0012 #include <QUrl>
0013 
0014 #include <KConfigGroup>
0015 #include <ksettings/dispatcher.h>
0016 
0017 #include <interfaces/iproject.h>
0018 
0019 #include "uploadprofileitem.h"
0020 
0021 UploadProfileModel::UploadProfileModel(QObject* parent)
0022     : QStandardItemModel(parent)
0023 {
0024     KSettings::Dispatcher::registerComponent(QStringLiteral("kdevupload"), this, "revert");
0025 }
0026 
0027 bool UploadProfileModel::removeRow(int row, const QModelIndex & parent)
0028 {
0029     UploadProfileItem* i = uploadItem(row);
0030     if (i && !i->profileNr().isEmpty()) {
0031         m_deltedProfileNrs << i->profileNr();
0032     }
0033     return QStandardItemModel::removeRow(row, parent);
0034 }
0035 
0036 UploadProfileItem* UploadProfileModel::uploadItem(int row, int column) const
0037 {
0038     QStandardItem* i = item(row, column);
0039     if (i) {
0040         return dynamic_cast<UploadProfileItem*>(i);
0041     }
0042     return nullptr;
0043 }
0044 UploadProfileItem* UploadProfileModel::uploadItem(const QModelIndex& index) const
0045 {
0046     QStandardItem* i = itemFromIndex(index);
0047     if (i) {
0048         return dynamic_cast<UploadProfileItem*>(i);
0049     }
0050     return nullptr;
0051 }
0052 void UploadProfileModel::setProject(KDevelop::IProject* project)
0053 {
0054     m_project = project;
0055     revert();
0056 }
0057 KDevelop::IProject* UploadProfileModel::project()
0058 {
0059     return m_project;
0060 }
0061 
0062 void UploadProfileModel::revert()
0063 {
0064     KConfigGroup group = m_project->projectConfiguration()->group("Upload");
0065     QString defProfile = group.readEntry("default", QString());
0066     int row = 0;
0067     Q_FOREACH (QString g, group.groupList()) {
0068         if (g.startsWith("Profile")) {
0069             QUrl url = group.group(g).readEntry("url", QUrl());
0070             QUrl localUrl = group.group(g).readEntry("localUrl", QUrl());
0071             QString name = group.group(g).readEntry("name", QString());
0072             UploadProfileItem* i = uploadItem(row);
0073             if (!i) {
0074                 i = new UploadProfileItem();
0075                 insertRow(row, i);
0076             }
0077             i->setText(name);
0078             i->setUrl(url);
0079             i->setLocalUrl(localUrl);
0080             i->setProfileNr(g.mid(7)); //group-name
0081             i->setDefault(i->profileNr() == defProfile);
0082             ++row;
0083         }
0084     }
0085     for (int i = row; i < rowCount(); ++i) {
0086         qDeleteAll(takeRow(i));
0087     }
0088 }
0089 
0090 bool UploadProfileModel::submit()
0091 {
0092     KConfigGroup group = m_project->projectConfiguration()->group("Upload");
0093     Q_FOREACH (QString i, m_deltedProfileNrs) {
0094         group.group("Profile" + i).deleteGroup();
0095     }
0096 
0097     int maxProfileNr = 0;
0098     for (int i = 0; i < rowCount(); i++) {
0099         UploadProfileItem* item = uploadItem(i);
0100         if (item) {
0101             maxProfileNr = qMax(item->profileNr().toInt(), maxProfileNr);
0102         }
0103     }
0104     QString defaultProfileNr;
0105     for (int i = 0; i < rowCount(); i++) {
0106         UploadProfileItem* item = uploadItem(i);
0107         if (item) {
0108             if (item->profileNr().isEmpty()) {
0109                 item->setProfileNr(QString::number(++maxProfileNr));
0110             }
0111             KConfigGroup profileGroup = group.group("Profile" + item->profileNr());
0112             profileGroup.writeEntry("url", item->url().toString());
0113             profileGroup.writeEntry("localUrl", item->localUrl().toString());
0114             profileGroup.writeEntry("name", item->text());
0115             if (item->isDefault()) {
0116                 defaultProfileNr = item->profileNr();
0117             }
0118         }
0119     }
0120     group.writeEntry("default", defaultProfileNr);
0121     return true;
0122 }
0123 
0124 
0125 // kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on