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>
43 OpenUrlDialog *OpenUrlDialog::instance = NULL;
45 OpenUrlDialog* OpenUrlDialog::getInstance( QWidget *parent,
46 intf_thread_t *p_intf,
51 instance = new OpenUrlDialog( parent, p_intf, bClipboard );
53 instance->bClipboard = bClipboard;
57 OpenUrlDialog::OpenUrlDialog( QWidget *parent,
58 intf_thread_t *_p_intf,
60 QVLCDialog( parent, _p_intf ), bClipboard( _bClipboard )
62 setWindowTitle( qtr( "Open URL" ) );
67 QDialogButtonBox *box = new QDialogButtonBox( this );
68 but = box->addButton( QDialogButtonBox::Ok );
69 CONNECT( but, clicked(), this, play() );
70 but = box->addButton( QDialogButtonBox::Cancel );
71 but = box->addButton( qtr( "&Enqueue" ), QDialogButtonBox::AcceptRole );
72 CONNECT( but, clicked(), this, enqueue() );
74 CONNECT( box, rejected(), this, reject() );
76 /* Info label and line edit */
77 edit = new ClickLineEdit( qtr( "Enter URL here..." ), this );
79 QLabel *info = new QLabel( qtr( "Please enter the URL or path "
80 "to the media you want to play"),
83 setToolTip( qtr( "If your clipboard contains a valid URL\n"
84 "or the path to a file on your computer,\n"
85 "it will be automatically selected." ) );
88 QVBoxLayout *vlay = new QVBoxLayout( this );
90 vlay->addWidget( info );
91 vlay->addWidget( edit );
92 vlay->addWidget( box );
95 void OpenUrlDialog::enqueue()
97 bShouldEnqueue = true;
98 lastUrl = edit->text();
102 void OpenUrlDialog::play()
104 lastUrl = edit->text();
108 QString OpenUrlDialog::url() const
113 bool OpenUrlDialog::shouldEnqueue() const
115 return bShouldEnqueue;
119 * When the dialog is shown, try to extract an URL from the clipboard
120 * and paste it in the Edit box.
121 * showEvent can happen not only on exec() but I think it's cool to
122 * actualize the URL on showEvent (eg. change virtual desktop...)
124 void OpenUrlDialog::showEvent( QShowEvent *ev )
127 bShouldEnqueue = false;
128 edit->setFocus( Qt::OtherFocusReason );
129 if( !lastUrl.isEmpty() && edit->text().isEmpty() )
131 /* The text should not have been changed, excepted if the user
132 has clicked Cancel before */
133 edit->setText( lastUrl );
140 QClipboard *clipboard = QApplication::clipboard();
141 assert( clipboard != NULL );
142 QString txt = clipboard->text( QClipboard::Selection ).trimmed();
144 if( txt.isEmpty() || ( !txt.contains("://") && !QFile::exists(txt) ) )
145 txt = clipboard->text( QClipboard::Clipboard ).trimmed();
147 if( txt.contains( "://" ) || QFile::exists( txt ) )
148 edit->setText( txt );