1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2011 Ricardo Villalba <rvm@escomposlinux.org>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "favorites.h"
|
---|
20 | #include "favoriteeditor.h"
|
---|
21 |
|
---|
22 | #include <QMenu>
|
---|
23 | #include <QAction>
|
---|
24 | #include <QSettings>
|
---|
25 | #include <QFile>
|
---|
26 | #include <QTextStream>
|
---|
27 | #include <QInputDialog>
|
---|
28 |
|
---|
29 | //#define FIRST_MENU_ENTRY 3
|
---|
30 | #define FIRST_MENU_ENTRY 2
|
---|
31 |
|
---|
32 | Favorites::Favorites(QString filename, QWidget * parent) : QObject(parent)
|
---|
33 | {
|
---|
34 | _filename = filename;
|
---|
35 | _menu = 0;
|
---|
36 |
|
---|
37 | parent_widget = parent;
|
---|
38 |
|
---|
39 | current_file = QString::null;
|
---|
40 | last_item = 1;
|
---|
41 |
|
---|
42 | edit_act = new QAction( "Edit...", this);
|
---|
43 | connect(edit_act, SIGNAL(triggered()), this, SLOT(edit()));
|
---|
44 |
|
---|
45 | jump_act = new QAction( "Jump...", this);
|
---|
46 | connect(jump_act, SIGNAL(triggered()), this, SLOT(jump()));
|
---|
47 |
|
---|
48 | next_act = new QAction(this);
|
---|
49 | connect(next_act, SIGNAL(triggered()), this, SLOT(next()));
|
---|
50 |
|
---|
51 | previous_act = new QAction(this);
|
---|
52 | connect(previous_act, SIGNAL(triggered()), this, SLOT(previous()));
|
---|
53 |
|
---|
54 | load();
|
---|
55 | }
|
---|
56 |
|
---|
57 | Favorites::~Favorites() {
|
---|
58 | save();
|
---|
59 | if (_menu != 0) delete _menu;
|
---|
60 | }
|
---|
61 |
|
---|
62 | QMenu * Favorites::menu() {
|
---|
63 | if (_menu == 0) createMenu();
|
---|
64 | return _menu;
|
---|
65 | }
|
---|
66 |
|
---|
67 | void Favorites::createMenu() {
|
---|
68 | _menu = new QMenu(parent_widget);
|
---|
69 | connect( _menu, SIGNAL(triggered(QAction *)),
|
---|
70 | this, SLOT(triggered_slot(QAction *)) );
|
---|
71 |
|
---|
72 | _menu->addAction(edit_act);
|
---|
73 | //_menu->addAction(jump_act);
|
---|
74 | _menu->addSeparator();
|
---|
75 |
|
---|
76 | populateMenu();
|
---|
77 | }
|
---|
78 |
|
---|
79 | void Favorites::populateMenu() {
|
---|
80 | for (int n = 0; n < f_list.count(); n++) {
|
---|
81 | QString i = QString::number(n+1);
|
---|
82 | QString name = QString("%1 - " + f_list[n].name() ).arg( i.insert( i.size()-1, '&' ), 3, ' ' );
|
---|
83 | QAction * a = _menu->addAction( name );
|
---|
84 | a->setData( f_list[n].file() );
|
---|
85 | a->setIcon( QIcon( f_list[n].icon() ) );
|
---|
86 | a->setStatusTip( f_list[n].file() );
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void Favorites::updateMenu() {
|
---|
91 | // Remove all except the first 2 items
|
---|
92 | while (_menu->actions().count() > FIRST_MENU_ENTRY) {
|
---|
93 | QAction * a = _menu->actions()[FIRST_MENU_ENTRY];
|
---|
94 | _menu->removeAction( a );
|
---|
95 | a->deleteLater();
|
---|
96 | }
|
---|
97 |
|
---|
98 | populateMenu();
|
---|
99 | markCurrent();
|
---|
100 | }
|
---|
101 |
|
---|
102 | void Favorites::triggered_slot(QAction * action) {
|
---|
103 | if (action->data().isValid()) {
|
---|
104 | QString file = action->data().toString();
|
---|
105 | emit activated( file );
|
---|
106 | current_file = file;;
|
---|
107 | markCurrent();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | void Favorites::markCurrent() {
|
---|
112 | for (int n = FIRST_MENU_ENTRY; n < _menu->actions().count(); n++) {
|
---|
113 | QAction * a = _menu->actions()[n];
|
---|
114 | QString file = a->data().toString();
|
---|
115 | QFont f = a->font();
|
---|
116 |
|
---|
117 | if (file == current_file) {
|
---|
118 | f.setBold(true);
|
---|
119 | a->setFont( f );
|
---|
120 | } else {
|
---|
121 | f.setBold(false);
|
---|
122 | a->setFont( f );
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | int Favorites::findFile(QString filename) {
|
---|
128 | for (int n = 0; n < f_list.count(); n++) {
|
---|
129 | if (f_list[n].file() == filename) return n;
|
---|
130 | }
|
---|
131 | return -1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | void Favorites::next() {
|
---|
135 | qDebug("Favorites::next");
|
---|
136 |
|
---|
137 | int current = findFile(current_file);
|
---|
138 | //if (current < 0) current = 0;
|
---|
139 |
|
---|
140 | current++;
|
---|
141 | if (current >= f_list.count()) current = 0;
|
---|
142 |
|
---|
143 | QAction * a = _menu->actions()[current+FIRST_MENU_ENTRY]; // Skip "edit" and separator
|
---|
144 | if (a != 0) {
|
---|
145 | a->trigger();
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | void Favorites::previous() {
|
---|
150 | qDebug("Favorites::previous");
|
---|
151 |
|
---|
152 | int current = findFile(current_file);
|
---|
153 | //if (current < 0) current = 0;
|
---|
154 |
|
---|
155 | current--;
|
---|
156 | if (current < 0) current = f_list.count()-1;
|
---|
157 |
|
---|
158 | QAction * a = _menu->actions()[current+FIRST_MENU_ENTRY]; // Skip "edit" and separator
|
---|
159 | if (a != 0) {
|
---|
160 | a->trigger();
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | void Favorites::save() {
|
---|
165 | qDebug("Favorites::save");
|
---|
166 |
|
---|
167 | QFile f( _filename );
|
---|
168 | if ( f.open( QIODevice::WriteOnly ) ) {
|
---|
169 | QTextStream stream( &f );
|
---|
170 | stream.setCodec("UTF-8");
|
---|
171 |
|
---|
172 | stream << "#EXTM3U" << "\n";
|
---|
173 | for (int n = 0; n < f_list.count(); n++) {
|
---|
174 | stream << "#EXTINF:0,";
|
---|
175 | stream << f_list[n].name() << ",";
|
---|
176 | stream << f_list[n].icon() << "\n";
|
---|
177 | stream << f_list[n].file() << "\n";
|
---|
178 | }
|
---|
179 | f.close();
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | void Favorites::load() {
|
---|
184 | qDebug("Favorites::load");
|
---|
185 |
|
---|
186 | QRegExp m3u_id("^#EXTM3U|^#M3U");
|
---|
187 | QRegExp info("^#EXTINF:(.*),(.*),(.*)");
|
---|
188 |
|
---|
189 | QFile f( _filename );
|
---|
190 | if ( f.open( QIODevice::ReadOnly ) ) {
|
---|
191 |
|
---|
192 | f_list.clear();
|
---|
193 |
|
---|
194 | Favorite fav;
|
---|
195 |
|
---|
196 | QTextStream stream( &f );
|
---|
197 | stream.setCodec("UTF-8");
|
---|
198 |
|
---|
199 | QString line;
|
---|
200 | while ( !stream.atEnd() ) {
|
---|
201 | line = stream.readLine(); // line of text excluding '\n'
|
---|
202 | //qDebug( " * line: '%s'", line.toUtf8().data() );
|
---|
203 | if (m3u_id.indexIn(line)!=-1) {
|
---|
204 | //#EXTM3U
|
---|
205 | // Ignore line
|
---|
206 | }
|
---|
207 | else
|
---|
208 | if (info.indexIn(line) != -1) {
|
---|
209 | fav.setName( info.cap(2) );
|
---|
210 | fav.setIcon( info.cap(3) );
|
---|
211 | }
|
---|
212 | else
|
---|
213 | if (line.startsWith("#")) {
|
---|
214 | // Comment
|
---|
215 | // Ignore
|
---|
216 | } else {
|
---|
217 | fav.setFile( line );
|
---|
218 | if (fav.name().isEmpty()) fav.setName(line);
|
---|
219 | //qDebug("Favorites::load: adding '%s' '%s'", fav.name().toUtf8().constData(), fav.file().toUtf8().constData());
|
---|
220 | f_list.append(fav);
|
---|
221 |
|
---|
222 | // Clear data
|
---|
223 | fav.setName("");
|
---|
224 | fav.setFile("");
|
---|
225 | fav.setIcon("");
|
---|
226 | }
|
---|
227 | }
|
---|
228 | f.close();
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | void Favorites::edit() {
|
---|
233 | qDebug("Favorites::edit");
|
---|
234 |
|
---|
235 | FavoriteEditor e(parent_widget);
|
---|
236 |
|
---|
237 | e.setData(f_list);
|
---|
238 |
|
---|
239 | if (e.exec() == QDialog::Accepted) {
|
---|
240 | f_list = e.data();
|
---|
241 | updateMenu();
|
---|
242 | /*
|
---|
243 | for (int n = 0; n < f_list.count(); n++) {
|
---|
244 | qDebug("item %d: name: '%s' file: '%s'", n, f_list[n].name().toUtf8().constData(), f_list[n].file().toUtf8().constData());
|
---|
245 | }
|
---|
246 | */
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | void Favorites::jump() {
|
---|
251 | qDebug("Favorites::jump");
|
---|
252 |
|
---|
253 | bool ok;
|
---|
254 | int item = QInputDialog::getInteger(parent_widget, tr("Jump to item"),
|
---|
255 | tr("Enter the number of the item in the list to jump:"),
|
---|
256 | last_item, 1, f_list.count(), 1, &ok);
|
---|
257 | if (ok) {
|
---|
258 | last_item = item;
|
---|
259 | item--;
|
---|
260 | _menu->actions()[item+FIRST_MENU_ENTRY]->trigger();
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | #include "moc_favorites.cpp"
|
---|
265 |
|
---|