• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

blogilo

  • sources
  • kde-4.14
  • kdepim
  • blogilo
  • src
uploadmediadialog.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Blogilo, A KDE Blogging Client
3 
4  Copyright (C) 2008-2010 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5  Copyright (C) 2008-2010 Golnaz Nilieh <g382nilieh@gmail.com>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License or (at your option) version 3 or any later version
11  accepted by the membership of KDE e.V. (or its successor approved
12  by the membership of KDE e.V.), which shall act as a proxy
13  defined in Section 14 of version 3 of the license.
14 
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, see http://www.gnu.org/licenses/
23 */
24 
25 #include "uploadmediadialog.h"
26 
27 #include "settings.h"
28 #include "bilboblog.h"
29 #include "bilbomedia.h"
30 #include "backend.h"
31 
32 #include <KApplication>
33 #include <KDebug>
34 #include <KMessageBox>
35 #include <KFileDialog>
36 #include <kio/jobclasses.h>
37 #include <kio/job.h>
38 
39 #include <QClipboard>
40 
41 UploadMediaDialog::UploadMediaDialog( QWidget *parent )
42  : KDialog(parent), mCurrentBlog(0)
43 {
44  QWidget *widget = new QWidget;
45  ui.setupUi(widget);
46  setMainWidget(widget);
47  setAttribute(Qt::WA_DeleteOnClose);
48 
49  setButtonText(KDialog::Ok, i18n("Upload") );
50  setWindowTitle( i18n( "Upload Media..." ) );
51  ui.kcfg_FtpPath->setText(Settings::ftpServerPath());
52  ui.kcfg_httpUrl->setText(Settings::httpUrl());
53  setWindowModality(Qt::ApplicationModal);
54  ui.kcfg_urlBrowser->setIcon(KIcon(QLatin1String("document-open")));
55  connect( ui.kcfg_urlBrowser, SIGNAL(clicked(bool)), this, SLOT(selectNewFile()) );
56  connect(ui.kcfg_uploadType, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUploadTypeChanged(int)));
57  connect( ui.kcfg_urlLineEdit, SIGNAL(textChanged(QString)), this, SLOT(currentMediaChanged(QString)) );
58 }
59 
60 UploadMediaDialog::~UploadMediaDialog()
61 {
62  Settings::setFtpServerPath(ui.kcfg_FtpPath->text());
63  Settings::setHttpUrl(ui.kcfg_httpUrl->text());
64  Settings::self()->writeConfig();
65 }
66 
67 void UploadMediaDialog::init( const BilboBlog *currentBlog )
68 {
69  if ( !selectNewFile() ) {
70  deleteLater();
71  return;
72  }
73  if (currentBlog) {
74  mCurrentBlog = currentBlog;
75  if (mCurrentBlog->supportUploadMedia()){
76  ui.kcfg_uploadType->addItem( i18n("Blog API"), BlogAPI );
77  }
78  }
79  ui.kcfg_uploadType->addItem( i18n("FTP"), FTP);
80  slotUploadTypeChanged(ui.kcfg_uploadType->currentIndex());
81  this->show();
82 }
83 
84 void UploadMediaDialog::currentMediaChanged(const QString& newPath)
85 {
86  ui.kcfg_previewer->showPreview(KUrl(newPath));
87 }
88 
89 bool UploadMediaDialog::selectNewFile()
90 {
91  const QString mediaPath = KFileDialog::getOpenFileName( KUrl("kfiledialog:///image?global"),
92  QString(), this,
93  i18n("Select Media to Upload"));
94  if ( mediaPath.isEmpty() )
95  return false;
96 
97  KUrl mediaUrl(mediaPath);
98  ui.kcfg_urlLineEdit->setText(mediaPath);
99  ui.kcfg_Name->setText(mediaUrl.fileName());
100  ui.kcfg_previewer->showPreview( mediaUrl );
101  return true;
102 }
103 
104 void UploadMediaDialog::slotUploadTypeChanged(int index)
105 {
106  UploadType type = static_cast<UploadType>(ui.kcfg_uploadType->itemData(index).toInt());
107  if (type == FTP) {
108  ui.kcfg_ftpBox->setEnabled(true);
109  } else {
110  ui.kcfg_ftpBox->setEnabled(false);
111  }
112 }
113 
114 void UploadMediaDialog::slotButtonClicked(int button)
115 {
116  if(button == KDialog::Ok) {
117  UploadType type = static_cast<UploadType>(ui.kcfg_uploadType->itemData(ui.kcfg_uploadType->currentIndex()).toInt());
118  if ( type == BlogAPI ) {
119  BilboMedia *media = new BilboMedia(this);
120  KUrl mediaUrl( ui.kcfg_urlLineEdit->text() );
121  media->setLocalUrl(mediaUrl);
122  media->setName( ui.kcfg_Name->text().isEmpty() ? mediaUrl.fileName() : ui.kcfg_Name->text() );
123  media->setBlogId( mCurrentBlog->id() );
124  media->setMimeType( KMimeType::findByUrl( mediaUrl, 0, true )->name() );
125  Backend *b = new Backend( mCurrentBlog->id(), this);
126  connect( b, SIGNAL(sigMediaUploaded(BilboMedia*)),
127  this, SLOT(slotMediaObjectUploaded(BilboMedia*)) );
128  connect( b, SIGNAL(sigError(QString)), this, SLOT(slotError(QString)));
129  connect( b, SIGNAL(sigMediaError(QString,BilboMedia*)), this, SLOT(slotError(QString)) );
130  b->uploadMedia( media );
131  this->hide();
132  emit sigBusy(true);
133  } else if ( type == FTP ) {
134  if ( ui.kcfg_FtpPath->text().isEmpty() ) {
135  KMessageBox::sorry(this, i18n("Please insert FTP URL."));
136  return;
137  }
138  KUrl dest;
139  dest.setUrl(ui.kcfg_FtpPath->text() , QUrl::TolerantMode);
140  if ( dest.isValid() ) {
141  if ( dest.scheme() == QLatin1String("ftp") || dest.scheme() == QLatin1String("sftp") ) {
142  KUrl src(ui.kcfg_urlLineEdit->text());
143  dest.addPath( ui.kcfg_Name->text().isEmpty() ? src.fileName() :
144  ui.kcfg_Name->text() );
145  KIO::FileCopyJob *job = KIO::file_copy(src, dest);
146  connect(job, SIGNAL(result(KJob*)), this, SLOT(slotMediaObjectUploaded(KJob*)));
147  job->start();
148  this->hide();
149  return;
150  }
151  }
152  KMessageBox::error(this, i18n("Inserted FTP URL is not a valid URL.\n"
153  "Note: The URL must start with \"ftp\" or \"sftp\", "
154  "and end with a \"/\" that indicates the directory to which the file should be uploaded."));
155  // > what is meant here?
156  // edited coles 2009 - I think it makes sense now.
157  }
158  } else {
159  KDialog::slotButtonClicked(button);
160  }
161 }
162 
163 void UploadMediaDialog::slotMediaObjectUploaded(KJob *job)
164 {
165  emit sigBusy(false);
166  if (job->error()) {
167  kDebug()<<"Job error: "<<job->errorString();
168  slotError(job->errorString());
169  } else {
170  KIO::FileCopyJob *fcj = qobject_cast<KIO::FileCopyJob*>(job);
171  KUrl tmpUrl(ui.kcfg_httpUrl->text());
172  QString destUrl;
173  if (tmpUrl.isValid()){
174  tmpUrl.adjustPath(KUrl::AddTrailingSlash);
175  tmpUrl.setFileName(ui.kcfg_Name->text());
176  destUrl = tmpUrl.prettyUrl();
177  } else {
178  destUrl = fcj->destUrl().prettyUrl();
179  }
180  QString msg;
181  if ( Settings::copyMediaUrl() ) {
182  KApplication::clipboard()->setText( destUrl );
183  msg = i18n( "Media uploaded, and URL copied to clipboard.\nYou can find it here:\n%1",
184  destUrl );
185  } else {
186  msg = i18n( "Media uploaded.\nYou can find it here:\n%1",
187  destUrl );
188  }
189  KMessageBox::information(this, msg, i18n( "Successfully uploaded" ), QString(), KMessageBox::AllowLink);
190  accept();
191  }
192 }
193 
194 void UploadMediaDialog::slotMediaObjectUploaded(BilboMedia *media)
195 {
196  QString msg;
197  emit sigBusy(false);
198  if ( Settings::copyMediaUrl() ) {
199  KApplication::clipboard()->setText( media->remoteUrl().prettyUrl() );
200  msg = i18n( "Media uploaded, and URL copied to clipboard.\nYou can find it here:\n%1",
201  media->remoteUrl().prettyUrl() );
202  } else {
203  msg = i18n( "Media uploaded.\nYou can find it here:\n%1",
204  media->remoteUrl().prettyUrl() );
205  }
206  KMessageBox::information(this, msg, i18n( "Successfully uploaded" ), QString(), KMessageBox::AllowLink);
207  accept();
208 }
209 
210 void UploadMediaDialog::slotError( const QString &msg )
211 {
212  emit sigBusy(false);
213  if ( KMessageBox::questionYesNo( this, i18n( "Media uploading failed with this result:\n%1\nTry again?", msg) )
214  == KMessageBox::Yes ) {
215  show();
216  } else {
217  reject();
218  }
219 }
220 
QWidget
BilboBlog::supportUploadMedia
bool supportUploadMedia() const
Definition: bilboblog.cpp:260
UploadMediaDialog::UploadType
UploadType
Definition: uploadmediadialog.h:39
UploadMediaDialog::UploadMediaDialog
UploadMediaDialog(QWidget *parent=0)
Definition: uploadmediadialog.cpp:41
KDialog
BilboMedia::setLocalUrl
void setLocalUrl(const KUrl &url)
Definition: bilbomedia.cpp:100
BilboMedia::setMimeType
void setMimeType(const QString &type)
Definition: bilbomedia.cpp:120
UploadMediaDialog::BlogAPI
Definition: uploadmediadialog.h:39
BilboBlog
Blog definition class!
Definition: bilboblog.h:40
uploadmediadialog.h
QString::isEmpty
bool isEmpty() const
bilbomedia.h
QString
bilboblog.h
UploadMediaDialog::~UploadMediaDialog
~UploadMediaDialog()
Definition: uploadmediadialog.cpp:60
UploadMediaDialog::FTP
Definition: uploadmediadialog.h:39
Backend::uploadMedia
void uploadMedia(BilboMedia *media)
Upload a new Media object e.g.
Definition: backend.cpp:191
Backend
Engine of application.
Definition: backend.h:45
UploadMediaDialog::sigBusy
void sigBusy(bool isBusy)
BilboMedia::setName
void setName(const QString &name)
Definition: bilbomedia.cpp:130
BilboMedia
Definition: bilbomedia.h:38
BilboBlog::id
int id() const
Definition: bilboblog.cpp:217
QLatin1String
UploadMediaDialog::init
void init(const BilboBlog *currentBlog)
Definition: uploadmediadialog.cpp:67
BilboMedia::setBlogId
void setBlogId(const int blog_id)
Definition: bilbomedia.cpp:70
backend.h
BilboMedia::remoteUrl
KUrl remoteUrl() const
Definition: bilbomedia.cpp:105
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:16 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

blogilo

Skip menu "blogilo"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal