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