]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/openurl.cpp
49a791b15478af2d230952ea36390064eb001b7b
[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::instance = NULL;
44
45 OpenUrlDialog* OpenUrlDialog::getInstance( QWidget *parent,
46                                            intf_thread_t *p_intf,
47                                            bool bClipboard )
48 {
49     /* Creation */
50     if( !instance )
51         instance = new OpenUrlDialog( parent, p_intf, bClipboard );
52     else
53         instance->bClipboard = bClipboard;
54     return instance;
55 }
56
57 OpenUrlDialog::OpenUrlDialog( QWidget *parent,
58                               intf_thread_t *_p_intf,
59                               bool _bClipboard ) :
60         QVLCDialog( parent, _p_intf ), bClipboard( _bClipboard )
61 {
62     setWindowTitle( qtr( "Open URL" ) );
63
64     /* Buttons */
65     QPushButton *but;
66
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() );
73
74     CONNECT( box, rejected(), this, reject() );
75
76     /* Info label and line edit */
77     edit = new ClickLineEdit( qtr( "Enter URL here..." ), this );
78
79     QLabel *info = new QLabel( qtr( "Please enter the URL or path "
80                                     "to the media you want to play"),
81                                this );
82
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." ) );
86
87     /* Layout */
88     QVBoxLayout *vlay = new QVBoxLayout( this );
89
90     vlay->addWidget( info );
91     vlay->addWidget( edit );
92     vlay->addWidget( box );
93 }
94
95 void OpenUrlDialog::enqueue()
96 {
97     bShouldEnqueue = true;
98     lastUrl = edit->text();
99     accept();
100 }
101
102 void OpenUrlDialog::play()
103 {
104     lastUrl = edit->text();
105     accept();
106 }
107
108 QString OpenUrlDialog::url() const
109 {
110     return lastUrl;
111 }
112
113 bool OpenUrlDialog::shouldEnqueue() const
114 {
115     return bShouldEnqueue;
116 }
117
118 /** Show Event:
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...)
123  **/
124 void OpenUrlDialog::showEvent( QShowEvent *ev )
125 {
126     (void) ev;
127     bShouldEnqueue = false;
128     edit->setFocus( Qt::OtherFocusReason );
129     if( !lastUrl.isEmpty() && edit->text().isEmpty() )
130     {
131         /* The text should not have been changed, excepted if the user
132         has clicked Cancel before */
133         edit->setText( lastUrl );
134     }
135     else
136         edit->clear();
137
138     if( bClipboard )
139     {
140         QClipboard *clipboard = QApplication::clipboard();
141         assert( clipboard != NULL );
142         QString txt = clipboard->text( QClipboard::Selection ).trimmed();
143
144         if( txt.isEmpty() || ( !txt.contains("://") && !QFile::exists(txt) ) )
145             txt = clipboard->text( QClipboard::Clipboard ).trimmed();
146
147         if( txt.contains( "://" ) || QFile::exists( txt ) )
148             edit->setText( txt );
149     }
150 }