32 #include <KApplication>
34 #include <KMessageBox>
35 #include <KFileDialog>
36 #include <kio/jobclasses.h>
42 :
KDialog(parent), mCurrentBlog(0)
46 setMainWidget(widget);
47 setAttribute(Qt::WA_DeleteOnClose);
49 setButtonText(KDialog::Ok, i18n(
"Upload") );
50 setWindowTitle( i18n(
"Upload Media..." ) );
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)) );
69 if ( !selectNewFile() ) {
74 mCurrentBlog = currentBlog;
76 ui.kcfg_uploadType->addItem( i18n(
"Blog API"),
BlogAPI );
79 ui.kcfg_uploadType->addItem( i18n(
"FTP"),
FTP);
80 slotUploadTypeChanged(ui.kcfg_uploadType->currentIndex());
84 void UploadMediaDialog::currentMediaChanged(
const QString& newPath)
86 ui.kcfg_previewer->showPreview(KUrl(newPath));
89 bool UploadMediaDialog::selectNewFile()
91 const QString mediaPath = KFileDialog::getOpenFileName( KUrl(
"kfiledialog:///image?global"),
93 i18n(
"Select Media to Upload"));
94 if ( mediaPath.isEmpty() )
97 KUrl mediaUrl(mediaPath);
98 ui.kcfg_urlLineEdit->setText(mediaPath);
99 ui.kcfg_Name->setText(mediaUrl.fileName());
100 ui.kcfg_previewer->showPreview( mediaUrl );
104 void UploadMediaDialog::slotUploadTypeChanged(
int index)
108 ui.kcfg_ftpBox->setEnabled(
true);
110 ui.kcfg_ftpBox->setEnabled(
false);
114 void UploadMediaDialog::slotButtonClicked(
int button)
116 if(button == KDialog::Ok) {
117 UploadType type =
static_cast<UploadType>(ui.kcfg_uploadType->itemData(ui.kcfg_uploadType->currentIndex()).toInt());
120 KUrl mediaUrl( ui.kcfg_urlLineEdit->text() );
122 media->
setName( ui.kcfg_Name->text().isEmpty() ? mediaUrl.fileName() : ui.kcfg_Name->text() );
124 media->
setMimeType( KMimeType::findByUrl( mediaUrl, 0,
true )->name() );
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)) );
133 }
else if ( type ==
FTP ) {
134 if ( ui.kcfg_FtpPath->text().isEmpty() ) {
135 KMessageBox::sorry(
this, i18n(
"Please insert FTP URL."));
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*)));
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."));
159 KDialog::slotButtonClicked(button);
163 void UploadMediaDialog::slotMediaObjectUploaded(KJob *job)
167 kDebug()<<
"Job error: "<<job->errorString();
168 slotError(job->errorString());
170 KIO::FileCopyJob *fcj = qobject_cast<KIO::FileCopyJob*>(job);
171 KUrl tmpUrl(ui.kcfg_httpUrl->text());
173 if (tmpUrl.isValid()){
174 tmpUrl.adjustPath(KUrl::AddTrailingSlash);
175 tmpUrl.setFileName(ui.kcfg_Name->text());
176 destUrl = tmpUrl.prettyUrl();
178 destUrl = fcj->destUrl().prettyUrl();
182 KApplication::clipboard()->setText( destUrl );
183 msg = i18n(
"Media uploaded, and URL copied to clipboard.\nYou can find it here:\n%1",
186 msg = i18n(
"Media uploaded.\nYou can find it here:\n%1",
189 KMessageBox::information(
this, msg, i18n(
"Successfully uploaded" ), QString(), KMessageBox::AllowLink);
194 void UploadMediaDialog::slotMediaObjectUploaded(
BilboMedia *media)
199 KApplication::clipboard()->setText( media->
remoteUrl().prettyUrl() );
200 msg = i18n(
"Media uploaded, and URL copied to clipboard.\nYou can find it here:\n%1",
203 msg = i18n(
"Media uploaded.\nYou can find it here:\n%1",
206 KMessageBox::information(
this, msg, i18n(
"Successfully uploaded" ), QString(), KMessageBox::AllowLink);
210 void UploadMediaDialog::slotError(
const QString &msg )
213 if ( KMessageBox::questionYesNo(
this, i18n(
"Media uploading failed with this result:\n%1\nTry again?", msg) )
214 == KMessageBox::Yes ) {
221 #include "uploadmediadialog.moc"
static void setFtpServerPath(const QString &v)
Set ftpServerPath.
bool supportUploadMedia() const
static QString httpUrl()
Get httpUrl.
static void setHttpUrl(const QString &v)
Set httpUrl.
static QString ftpServerPath()
Get ftpServerPath.
void uploadMedia(BilboMedia *media)
Upload a new Media object e.g.
static bool copyMediaUrl()
Get CopyMediaUrl.