]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/openurl.cpp
qt4: attempt to work around a design flaw in the toolbar editor
[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( qtr( "&Play" ), QDialogButtonBox::AcceptRole );
55     CONNECT( but, clicked(), this, play() );
56
57     but = box->addButton( qtr( "&Enqueue" ), QDialogButtonBox::AcceptRole );
58     CONNECT( but, clicked(), this, enqueue() );
59
60     but = box->addButton( qtr( "&Cancel" ) , QDialogButtonBox::RejectRole );
61     CONNECT( box, rejected(), this, reject() );
62
63     /* Info label and line edit */
64     edit = new ClickLineEdit( qtr( "Enter URL here..." ), this );
65
66     QLabel *info = new QLabel( qtr( "Please enter the URL or path "
67                                     "to the media you want to play"),
68                                this );
69
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." ) );
73
74     /* Layout */
75     QVBoxLayout *vlay = new QVBoxLayout( this );
76
77     vlay->addWidget( info );
78     vlay->addWidget( edit );
79     vlay->addWidget( box );
80 }
81
82 void OpenUrlDialog::enqueue()
83 {
84     bShouldEnqueue = true;
85     lastUrl = edit->text();
86     accept();
87 }
88
89 void OpenUrlDialog::play()
90 {
91     lastUrl = edit->text();
92     accept();
93 }
94
95 QString OpenUrlDialog::url() const
96 {
97     return lastUrl;
98 }
99
100 bool OpenUrlDialog::shouldEnqueue() const
101 {
102     return bShouldEnqueue;
103 }
104
105 /** Show Event:
106  * When the dialog is shown, try to extract an 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...)
110  **/
111 void OpenUrlDialog::showEvent( QShowEvent *ev )
112 {
113     (void) ev;
114     bShouldEnqueue = false;
115     edit->setFocus( Qt::OtherFocusReason );
116     if( !lastUrl.isEmpty() && edit->text().isEmpty() )
117     {
118         /* The text should not have been changed, excepted if the user
119         has clicked Cancel before */
120         edit->setText( lastUrl );
121     }
122     else
123         edit->clear();
124
125     if( bClipboard )
126     {
127         QClipboard *clipboard = QApplication::clipboard();
128         assert( clipboard != NULL );
129         QString txt = clipboard->text( QClipboard::Selection ).trimmed();
130
131         if( txt.isEmpty() || ( !txt.contains("://") && !QFile::exists(txt) ) )
132             txt = clipboard->text( QClipboard::Clipboard ).trimmed();
133
134         if( txt.contains( "://" ) || QFile::exists( txt ) )
135             edit->setText( txt );
136     }
137 }