File indexing completed on 2024-04-21 04:35:57

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 "profilesfiletree.h"
0011 
0012 #include <QVBoxLayout>
0013 #include <QComboBox>
0014 #include <QHeaderView>
0015 #include <QLabel>
0016 #include <QPushButton>
0017 #include <QMenu>
0018 #include <QClipboard>
0019 #include <QApplication>
0020 #include <QDesktopServices>
0021 #include "kdevuploaddebug.h"
0022 
0023 #include <KLocalizedString>
0024 #include <KDirOperator>
0025 #include <KFileWidget>
0026 #include <KActionCollection>
0027 #include <kfileitem.h>
0028 
0029 #include <interfaces/icore.h>
0030 #include <interfaces/idocument.h>
0031 #include <interfaces/idocumentcontroller.h>
0032 
0033 #include "allprofilesmodel.h"
0034 #include "uploadprofileitem.h"
0035 #include "uploadprofiledlg.h"
0036 
0037 ProfilesFileTree::ProfilesFileTree(UploadPlugin* plugin, QWidget *parent)
0038     : QWidget(parent), m_plugin(plugin), m_editProfileDlg(nullptr)
0039 {
0040     QVBoxLayout *l = new QVBoxLayout();
0041     setLayout(l);
0042 
0043     QHBoxLayout *hl = new QHBoxLayout();
0044     l->addLayout(hl);
0045 
0046     m_profilesCombo = new QComboBox;
0047     QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0048     sizePolicy.setHorizontalStretch(1);
0049     m_profilesCombo->setSizePolicy(sizePolicy);
0050     hl->addWidget(m_profilesCombo);
0051 
0052     connect(m_profilesCombo, SIGNAL(currentIndexChanged(int)),
0053             this, SLOT(profileIndexChanged(int)));
0054 
0055     QPushButton* editButton = new QPushButton("...");    
0056     hl->addWidget(editButton);
0057 
0058     connect(editButton, SIGNAL(clicked()), this, SLOT(modifyProfile()));
0059 
0060     m_pleaseSelectLabel = new QLabel(i18n("Please choose an upload profile."));
0061     m_pleaseSelectLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
0062     l->addWidget(m_pleaseSelectLabel);
0063 
0064 
0065     m_tree = new KDirOperator();
0066     m_tree->setView(KFile::Tree);
0067     connect(m_tree, SIGNAL(fileSelected(const KFileItem &)),
0068            SLOT(fileSelected(KFileItem)));
0069     connect(m_tree, SIGNAL(urlEntered()), SLOT(urlEntered()), Qt::QueuedConnection);
0070     connect(m_tree, SIGNAL(contextMenuAboutToShow(KFileItem,QMenu*)), SLOT(contextMenuAboutToShow(KFileItem,QMenu*)));
0071 
0072     QAction* a = new QAction(i18n("&Copy URL"), this);
0073     connect(a, SIGNAL(triggered(bool)), SLOT(copyUrl()));
0074     m_tree->actionCollection()->addAction("copyUrl", a);
0075     a = new QAction(i18n("&Open URL"), this);
0076     connect(a, SIGNAL(triggered(bool)), SLOT(browseUrl()));
0077     m_tree->actionCollection()->addAction("browse", a);
0078 
0079     l->addWidget(m_tree);
0080 
0081 }
0082 
0083 void ProfilesFileTree::browseUrl()
0084 {
0085     QUrl url;
0086     if (m_tree->selectedItems().isEmpty()) {
0087         url = m_tree->url();
0088     } else {
0089         url = m_tree->selectedItems().first().url();
0090     }
0091     QDesktopServices::openUrl(url);
0092 }
0093 
0094 void ProfilesFileTree::copyUrl()
0095 {
0096     QUrl url;
0097     if (m_tree->selectedItems().isEmpty()) {
0098         url = m_tree->url();
0099     } else {
0100         url = m_tree->selectedItems().first().url();
0101     }
0102     QApplication::clipboard()->setText(url.url());
0103 }
0104 
0105 
0106 void ProfilesFileTree::contextMenuAboutToShow(KFileItem item, QMenu* menu)
0107 {
0108     m_tree->setupMenu(KDirOperator::FileActions);
0109     menu->addSeparator();
0110     if (item.isDir()) {
0111         menu->addAction(m_tree->actionCollection()->action("browse"));
0112     }
0113     menu->addAction(m_tree->actionCollection()->action("copyUrl"));
0114 }
0115 
0116 
0117 void ProfilesFileTree::urlEntered()
0118 {
0119     profileIndexChanged(m_profilesCombo->currentIndex());
0120 }
0121 
0122 void ProfilesFileTree::fileSelected(const KFileItem& item)
0123 {
0124     openUrl(item.url());
0125 }
0126 
0127 
0128 void ProfilesFileTree::profileIndexChanged(int index)
0129 {
0130     if (index == -1) {
0131         m_tree->setVisible(false);
0132         m_pleaseSelectLabel->setVisible(true);
0133         return;
0134     }
0135 
0136     UploadProfileItem* item = m_profilesModel->uploadItem(
0137                             m_profilesModel->index(index, 0, QModelIndex()));
0138     if (item) {
0139         qCDebug(KDEVUPLOAD) << "item->url()" << item->url();
0140         m_tree->setVisible(true);
0141         m_pleaseSelectLabel->setVisible(false);
0142         qCDebug(KDEVUPLOAD) << "m_tree->url()" << m_tree->url();
0143         if (m_tree->url() != item->url()) {
0144             m_tree->setUrl(item->url(), true);
0145         }
0146     } else {
0147         profileIndexChanged(-1);
0148     }
0149 }
0150 
0151 void ProfilesFileTree::openUrl(const QUrl& url)
0152 {
0153     qCDebug(KDEVUPLOAD) << "openUrl" << url;
0154     KDevelop::ICore::self()->documentController()->openDocument(url);
0155 }
0156 void ProfilesFileTree::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
0157 {
0158     Q_UNUSED(topLeft);
0159     Q_UNUSED(bottomRight);
0160     profileIndexChanged(m_profilesCombo->currentIndex());
0161 }
0162 
0163 void ProfilesFileTree::modifyProfile()
0164 {
0165     UploadProfileItem* i = m_profilesModel->uploadItem(m_profilesCombo->currentIndex());
0166     if (i) {
0167         if (!m_editProfileDlg) {
0168             m_editProfileDlg = new UploadProfileDlg(this);
0169         }
0170         if (m_editProfileDlg->editProfile(i) == QDialog::Accepted) {
0171             i->model()->submit();
0172         }
0173     }
0174 }
0175 
0176 void ProfilesFileTree::setModel(AllProfilesModel* model)
0177 {
0178     m_profilesModel = model;
0179 
0180     m_profilesCombo->setModel(m_profilesModel);
0181     connect(m_profilesModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
0182             this, SLOT(dataChanged(QModelIndex, QModelIndex)));
0183 }
0184     
0185     
0186 
0187 // kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on