File indexing completed on 2022-08-05 15:23:19

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 "uploaddialog.h"
0011 
0012 #include <QPushButton>
0013 #include <QHeaderView>
0014 #include <QProgressDialog>
0015 #include <QMenu>
0016 #include <QContextMenuEvent>
0017 
0018 #include <KLocalizedString>
0019 
0020 #include <kconfiggroup.h>
0021 #include <kmessagebox.h>
0022 #include <kio/job.h>
0023 #include <kio/copyjob.h>
0024 #include <kio/jobuidelegate.h>
0025 #include <kparts/mainwindow.h>
0026 #include <kjobwidgets.h>
0027 
0028 #include <interfaces/icore.h>
0029 #include <interfaces/iuicontroller.h>
0030 #include <interfaces/iproject.h>
0031 #include <interfaces/iprojectcontroller.h>
0032 #include <project/projectmodel.h>
0033 
0034 #include "ui_uploaddialog.h"
0035 #include "uploadprojectmodel.h"
0036 #include "uploadprofilemodel.h"
0037 #include "uploadprofileitem.h"
0038 #include "uploadprofiledlg.h"
0039 #include "uploadjob.h"
0040 #include "kdevuploadplugin.h"
0041 
0042 UploadDialog::UploadDialog(KDevelop::IProject* project, UploadPlugin* plugin, QWidget *parent)
0043     : QDialog(parent), m_project(project), m_profileModel(nullptr), m_editProfileDlg(nullptr), m_plugin(plugin)
0044 {
0045     m_ui = new Ui::UploadDialog();
0046     m_ui->setupUi(this);
0047 
0048     m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("&Upload"));
0049     connect(m_ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
0050             this, SLOT(startUpload()));
0051 
0052     m_uploadProjectModel = new UploadProjectModel(project);
0053     m_uploadProjectModel->setSourceModel(project->projectItem()->model());
0054     m_ui->projectTree->setModel(m_uploadProjectModel);
0055     m_ui->projectTree->header()->hide();
0056 
0057     connect(m_ui->profileCombobox, SIGNAL(currentIndexChanged(int)),
0058             this, SLOT(profileChanged(int)));
0059 
0060     m_ui->profileCombobox->setCurrentIndex(-1);
0061 
0062     connect(m_ui->modifyProfileButton, SIGNAL(clicked()),
0063             this, SLOT(modifyProfile()));
0064 
0065 
0066     m_treeContextMenu = new QMenu(this);
0067     QAction* action = new QAction(i18nc("Select all items in the tree", "All"), this);
0068     connect(action, SIGNAL(triggered()), m_uploadProjectModel, SLOT(checkAll()));
0069     m_treeContextMenu->addAction(action);
0070 
0071     action = new QAction(i18n("Modified"), this);
0072     connect(action, SIGNAL(triggered()), m_uploadProjectModel, SLOT(checkModified()));
0073     m_treeContextMenu->addAction(action);
0074 
0075     action = new QAction(i18n("Invert"), this);
0076     connect(action, SIGNAL(triggered()), m_uploadProjectModel, SLOT(checkInvert()));
0077     m_treeContextMenu->addAction(action);
0078 
0079     m_ui->projectTree->installEventFilter(this);
0080 }
0081 
0082 UploadDialog::~UploadDialog()
0083 {
0084     delete m_ui;
0085     delete m_editProfileDlg;
0086 }
0087 
0088 void UploadDialog::modifyProfile()
0089 {
0090     UploadProfileItem* i = m_profileModel->uploadItem(m_ui->profileCombobox->currentIndex());
0091     if (i) {
0092         if (!m_editProfileDlg) {
0093             m_editProfileDlg = new UploadProfileDlg(this);
0094         }
0095         if (m_editProfileDlg->editProfile(i) == QDialog::Accepted) {
0096             m_profileModel->submit();
0097         }
0098     }
0099 }
0100 
0101 void UploadDialog::profileChanged(int index)
0102 {
0103     UploadProfileItem* i = m_profileModel->uploadItem(index);
0104     if (i) {
0105         KConfigGroup c = i->profileConfigGroup();
0106         if (c.isValid()) {
0107             m_uploadProjectModel->setProfileConfigGroup(c);
0108             m_ui->projectTree->setEnabled(true);
0109             m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
0110             return;
0111         }
0112     }
0113     m_ui->projectTree->setEnabled(false);
0114     m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0115 }
0116 
0117 void UploadDialog::startUpload()
0118 {
0119     
0120     if (m_ui->profileCombobox->currentIndex() == -1) {
0121         KMessageBox::error(this, i18n("Cannot upload, no profile selected."));
0122         return;
0123     }
0124     UploadJob* job = new UploadJob(m_project, m_uploadProjectModel, this);
0125     connect(job, SIGNAL(uploadFinished()), this, SLOT(uploadFinished()));
0126     job->setOnlyMarkUploaded(m_ui->markUploadedCheckBox->checkState() == Qt::Checked);
0127     job->setOutputModel(m_plugin->outputModel());
0128     job->start();
0129 }
0130 
0131 void UploadDialog::uploadFinished()
0132 {
0133     hide();
0134 }
0135 
0136 void UploadDialog::setRootItem(KDevelop::ProjectBaseItem* item)
0137 {
0138     m_uploadProjectModel->setRootItem(item);
0139     if (item) {
0140         QModelIndex i = m_uploadProjectModel->mapFromSource(item->index());
0141         while (i.isValid()) {
0142             m_ui->projectTree->expand(i);
0143             i = i.parent();
0144         }
0145     }
0146 }
0147 
0148 bool UploadDialog::eventFilter(QObject* obj, QEvent* event)
0149 {
0150     if (event->type() == QEvent::ContextMenu) {
0151         QContextMenuEvent *menuEvent = static_cast<QContextMenuEvent*>(event);
0152         m_treeContextMenu->exec(menuEvent->globalPos());
0153         return true;
0154     } else {
0155         // standard event processing
0156         return QObject::eventFilter(obj, event);
0157     }
0158 }
0159 
0160 void UploadDialog::setProfileModel(UploadProfileModel* model)
0161 {
0162     m_profileModel = model;
0163     m_ui->profileCombobox->setModel(model);
0164     for (int i = 0; i < m_profileModel->rowCount(); i++) {
0165         if (m_profileModel->uploadItem(i)->isDefault()) {
0166             m_ui->profileCombobox->setCurrentIndex(i);
0167             break;
0168         }
0169     }
0170 }
0171 // kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on