1 /*****************************************************************************
2 * openurl.cpp: Open a MRL or clipboard content
3 *****************************************************************************
4 * Copyright © 2009 the VideoLAN team
7 * Authors: Jean-Philippe André <jpeg@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include "dialogs/openurl.hpp"
29 #include "util/customwidgets.hpp"
31 #include <QPushButton>
32 #include <QDialogButtonBox>
33 #include <QApplication>
41 OpenUrlDialog *OpenUrlDialog::instance = NULL;
43 OpenUrlDialog* OpenUrlDialog::getInstance( QWidget *parent,
44 intf_thread_t *p_intf,
49 instance = new OpenUrlDialog( parent, p_intf, bClipboard );
51 instance->bClipboard = bClipboard;
55 OpenUrlDialog::OpenUrlDialog( QWidget *parent,
56 intf_thread_t *_p_intf,
57 bool bClipboard ) : QVLCDialog( parent, _p_intf )
59 this->bClipboard = bClipboard;
60 this->setWindowTitle( qtr( "Open URL" ) );
65 QDialogButtonBox *box = new QDialogButtonBox( this );
66 but = box->addButton( QDialogButtonBox::Ok );
67 CONNECT( but, clicked(), this, play() );
68 but = box->addButton( QDialogButtonBox::Cancel );
69 but = box->addButton( qtr( "&Enqueue" ), QDialogButtonBox::AcceptRole );
70 CONNECT( but, clicked(), this, enqueue() );
72 CONNECT( box, rejected(), this, reject() );
74 /* Info label and line edit */
75 edit = new ClickLineEdit( qtr( "Enter URL here..." ), this );
77 QLabel *info = new QLabel( qtr( "Please enter the URL or path "
78 "to the media you want to play"),
81 this->setToolTip( qtr( "If your clipboard contains a valid URL\n"
82 "or the path to a file on your computer,\n"
83 "it will be automatically selected." ) );
86 QVBoxLayout *vlay = new QVBoxLayout( this );
88 vlay->addWidget( info );
89 vlay->addWidget( edit );
90 vlay->addWidget( box );
93 void OpenUrlDialog::enqueue()
95 bShouldEnqueue = true;
96 lastUrl = edit->text();
100 void OpenUrlDialog::play()
102 lastUrl = edit->text();
106 QString OpenUrlDialog::url() const
111 bool OpenUrlDialog::shouldEnqueue() const
113 return bShouldEnqueue;
117 * When the dialog is shown, try to extract an URL from the clipboard
118 * and paste it in the Edit box.
119 * showEvent can happen not only on exec() but I think it's cool to
120 * actualize the URL on showEvent (eg. change virtual desktop...)
122 void OpenUrlDialog::showEvent( QShowEvent *ev )
125 bShouldEnqueue = false;
126 edit->setFocus( Qt::OtherFocusReason );
127 if( !lastUrl.isEmpty() && edit->text().isEmpty() )
129 /* The text should not have been changed, excepted if the user
130 has clicked Cancel before */
131 edit->setText( lastUrl );
138 QClipboard *clipboard = QApplication::clipboard();
139 const QMimeData *data = clipboard->mimeData( QClipboard::Selection );
140 QString txt = data->text().trimmed();
142 if( txt.isEmpty() || ( !txt.contains("://") && !QFile::exists(txt) ) )
143 txt = clipboard->mimeData()->text().trimmed();
145 if( txt.contains( "://" ) || QFile::exists( txt ) )
146 edit->setText( txt );