]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/openurl.cpp
d58889825930f9c5562c92f32fece940ed9f7962
[vlc] / modules / gui / qt4 / dialogs / openurl.cpp
1 /*****************************************************************************
2  * openurl.cpp: Open a MRL or clipboard content
3  *****************************************************************************
4  * Copyright © 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Philippe André <jpeg@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "dialogs/openurl.hpp"
29 #include "util/customwidgets.hpp"
30
31 #include <QPushButton>
32 #include <QDialogButtonBox>
33 #include <QApplication>
34 #include <QClipboard>
35 #include <QMimeData>
36 #include <QList>
37 #include <QUrl>
38 #include <QFile>
39 #include <QLabel>
40
41 #include <assert.h>
42
43 OpenUrlDialog::OpenUrlDialog( intf_thread_t *_p_intf,
44                               bool _bClipboard ) :
45         QVLCDialog( (QWidget*)_p_intf->p_sys->p_mi, _p_intf ), bClipboard( _bClipboard )
46 {
47     setWindowTitle( qtr( "Open URL" ) );
48     setWindowRole( "vlc-open-url" );
49
50     /* Buttons */
51     QPushButton *but;
52
53     QDialogButtonBox *box = new QDialogButtonBox( this );
54     but = box->addButton( QDialogButtonBox::Ok );
55     CONNECT( but, clicked(), this, play() );
56     but = box->addButton( QDialogButtonBox::Cancel );
57     but = box->addButton( qtr( "&Enqueue" ), QDialogButtonBox::AcceptRole );
58     CONNECT( but, clicked(), this, enqueue() );
59
60     CONNECT( box, rejected(), this, reject() );
61
62     /* Info label and line edit */
63     edit = new ClickLineEdit( qtr( "Enter URL here..." ), this );
64
65     QLabel *info = new QLabel( qtr( "Please enter the URL or path "
66                                     "to the media you want to play"),
67                                this );
68
69     setToolTip( qtr( "If your clipboard contains a valid URL\n"
70                      "or the path to a file on your computer,\n"
71                      "it will be automatically selected." ) );
72
73     /* Layout */
74     QVBoxLayout *vlay = new QVBoxLayout( this );
75
76     vlay->addWidget( info );
77     vlay->addWidget( edit );
78     vlay->addWidget( box );
79 }
80
81 void OpenUrlDialog::enqueue()
82 {
83     bShouldEnqueue = true;
84     lastUrl = edit->text();
85     accept();
86 }
87
88 void OpenUrlDialog::play()
89 {
90     lastUrl = edit->text();
91     accept();
92 }
93
94 QString OpenUrlDialog::url() const
95 {
96     return lastUrl;
97 }
98
99 bool OpenUrlDialog::shouldEnqueue() const
100 {
101     return bShouldEnqueue;
102 }
103
104 /** Show Event:
105  * When the dialog is shown, try to extract an URL from the clipboard
106  * and paste it in the Edit box.
107  * showEvent can happen not only on exec() but I think it's cool to
108  * actualize the URL on showEvent (eg. change virtual desktop...)
109  **/
110 void OpenUrlDialog::showEvent( QShowEvent *ev )
111 {
112     (void) ev;
113     bShouldEnqueue = false;
114     edit->setFocus( Qt::OtherFocusReason );
115     if( !lastUrl.isEmpty() && edit->text().isEmpty() )
116     {
117         /* The text should not have been changed, excepted if the user
118         has clicked Cancel before */
119         edit->setText( lastUrl );
120     }
121     else
122         edit->clear();
123
124     if( bClipboard )
125     {
126         QClipboard *clipboard = QApplication::clipboard();
127         assert( clipboard != NULL );
128         QString txt = clipboard->text( QClipboard::Selection ).trimmed();
129
130         if( txt.isEmpty() || ( !txt.contains("://") && !QFile::exists(txt) ) )
131             txt = clipboard->text( QClipboard::Clipboard ).trimmed();
132
133         if( txt.contains( "://" ) || QFile::exists( txt ) )
134             edit->setText( txt );
135     }
136 }