]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/openurl.cpp
5fa3e7b19b8025ab5925376b82877a5910defcef
[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 OpenUrlDialog *OpenUrlDialog::instance = NULL;
42
43 OpenUrlDialog* OpenUrlDialog::getInstance( QWidget *parent,
44                                            intf_thread_t *p_intf,
45                                            bool bClipboard )
46 {
47     /* Creation */
48     if( !instance )
49         instance = new OpenUrlDialog( parent, p_intf, bClipboard );
50     else
51         instance->bClipboard = bClipboard;
52     return instance;
53 }
54
55 OpenUrlDialog::OpenUrlDialog( QWidget *parent,
56                               intf_thread_t *_p_intf,
57                               bool bClipboard ) : QVLCDialog( parent, _p_intf )
58 {
59     this->bClipboard = bClipboard;
60     this->setWindowTitle( qtr( "Open URL" ) );
61
62     /* Buttons */
63     QPushButton *but;
64
65     QDialogButtonBox *box = new QDialogButtonBox( this );
66     but = box->addButton( QDialogButtonBox::Ok );
67     CONNECT( but, clicked(), this, play() );
68     but = box->addButton( QDialogButtonBox::Cancel );
69     but = box->addButton( qtr( "&Enqueue" ), QDialogButtonBox::AcceptRole );
70     CONNECT( but, clicked(), this, enqueue() );
71
72     CONNECT( box, rejected(), this, reject() );
73
74     /* Info label and line edit */
75     edit = new ClickLineEdit( qtr( "Enter URL here..." ), this );
76
77     QLabel *info = new QLabel( qtr( "Please enter the URL or path "
78                                     "to the media you want to play"),
79                                this );
80
81     this->setToolTip( qtr( "If your clipboard contains a valid URL\n"
82                            "or the path to a file on your computer,\n"
83                            "it will be automatically selected." ) );
84
85     /* Layout */
86     QVBoxLayout *vlay = new QVBoxLayout( this );
87
88     vlay->addWidget( info );
89     vlay->addWidget( edit );
90     vlay->addWidget( box );
91 }
92
93 void OpenUrlDialog::enqueue()
94 {
95     bShouldEnqueue = true;
96     lastUrl = edit->text();
97     accept();
98 }
99
100 void OpenUrlDialog::play()
101 {
102     lastUrl = edit->text();
103     accept();
104 }
105
106 QString OpenUrlDialog::url() const
107 {
108     return lastUrl;
109 }
110
111 bool OpenUrlDialog::shouldEnqueue() const
112 {
113     return bShouldEnqueue;
114 }
115
116 /** Show Event:
117  * When the dialog is shown, try to extract an URL from the clipboard
118  * and paste it in the Edit box.
119  * showEvent can happen not only on exec() but I think it's cool to
120  * actualize the URL on showEvent (eg. change virtual desktop...)
121  **/
122 void OpenUrlDialog::showEvent( QShowEvent *ev )
123 {
124     (void) ev;
125     bShouldEnqueue = false;
126     edit->setFocus( Qt::OtherFocusReason );
127     if( !lastUrl.isEmpty() && edit->text().isEmpty() )
128     {
129         /* The text should not have been changed, excepted if the user
130         has clicked Cancel before */
131         edit->setText( lastUrl );
132     }
133     else
134         edit->clear();
135
136     if( bClipboard )
137     {
138         QClipboard *clipboard = QApplication::clipboard();
139         const QMimeData *data = clipboard->mimeData( QClipboard::Selection );
140         QString txt = data->text().trimmed();
141
142         if( txt.isEmpty() || ( !txt.contains("://") && !QFile::exists(txt) ) )
143             txt = clipboard->mimeData()->text().trimmed();
144
145         if( txt.contains( "://" ) || QFile::exists( txt ) )
146             edit->setText( txt );
147     }
148 }