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/searchlineedit.hpp"
31 #include <QPushButton>
32 #include <QDialogButtonBox>
33 #include <QApplication>
43 OpenUrlDialog::OpenUrlDialog( intf_thread_t *_p_intf,
45 QVLCDialog( (QWidget*)_p_intf->p_sys->p_mi, _p_intf ), bClipboard( _bClipboard )
47 setWindowTitle( qtr( "Open URL" ) );
48 setWindowRole( "vlc-open-url" );
53 QDialogButtonBox *box = new QDialogButtonBox( this );
54 but = box->addButton( qtr( "&Play" ), QDialogButtonBox::AcceptRole );
55 CONNECT( but, clicked(), this, play() );
57 but = box->addButton( qtr( "&Enqueue" ), QDialogButtonBox::AcceptRole );
58 CONNECT( but, clicked(), this, enqueue() );
60 but = box->addButton( qtr( "&Cancel" ) , QDialogButtonBox::RejectRole );
61 CONNECT( box, rejected(), this, reject() );
63 /* Info label and line edit */
64 edit = new ClickLineEdit( qtr( "Enter URL here..." ), this );
66 QLabel *info = new QLabel( qtr( "Please enter the URL or path "
67 "to the media you want to play"),
70 setToolTip( qtr( "If your clipboard contains a valid URL\n"
71 "or the path to a file on your computer,\n"
72 "it will be automatically selected." ) );
75 QVBoxLayout *vlay = new QVBoxLayout( this );
77 vlay->addWidget( info );
78 vlay->addWidget( edit );
79 vlay->addWidget( box );
82 void OpenUrlDialog::enqueue()
84 bShouldEnqueue = true;
85 lastUrl = edit->text();
89 void OpenUrlDialog::play()
91 lastUrl = edit->text();
95 QString OpenUrlDialog::url() const
100 bool OpenUrlDialog::shouldEnqueue() const
102 return bShouldEnqueue;
106 * When the dialog is shown, try to extract a URL from the clipboard
107 * and paste it in the Edit box.
108 * showEvent can happen not only on exec() but I think it's cool to
109 * actualize the URL on showEvent (eg. change virtual desktop...)
111 void OpenUrlDialog::showEvent( QShowEvent *ev )
114 bShouldEnqueue = false;
115 edit->setFocus( Qt::OtherFocusReason );
116 if( !lastUrl.isEmpty() && edit->text().isEmpty() )
118 /* The text should not have been changed, excepted if the user
119 has clicked Cancel before */
120 edit->setText( lastUrl );
127 QClipboard *clipboard = QApplication::clipboard();
128 assert( clipboard != NULL );
129 QString txt = clipboard->text( QClipboard::Selection ).trimmed();
131 if( txt.isEmpty() || ( !txt.contains("://") && !QFile::exists(txt) ) )
132 txt = clipboard->text( QClipboard::Clipboard ).trimmed();
134 if( txt.contains( "://" ) || QFile::exists( txt ) )
135 edit->setText( txt );