source: smplayer/trunk/src/favorites.cpp@ 132

Last change on this file since 132 was 124, checked in by Silvan Scherrer, 13 years ago

SMPlayer: 0.7.1 trunk update

  • Property svn:eol-style set to LF
File size: 9.0 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2012 Ricardo Villalba <rvm@users.sourceforge.net>
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 <QAction>
23#include <QSettings>
24#include <QFile>
25#include <QTextStream>
26#include <QInputDialog>
27#include <QFileInfo>
28
29//#define FIRST_MENU_ENTRY 4
30#define FIRST_MENU_ENTRY 3
31
32Favorites::Favorites(QString filename, QWidget * parent) : QMenu(parent)
33{
34 _filename = filename;
35
36 parent_widget = parent;
37
38 current_file = QString::null;
39 last_item = 1;
40
41 edit_act = new QAction( "Edit...", this);
42 connect(edit_act, SIGNAL(triggered()), this, SLOT(edit()));
43
44 jump_act = new QAction( "Jump...", this);
45 connect(jump_act, SIGNAL(triggered()), this, SLOT(jump()));
46
47 next_act = new QAction(this);
48 connect(next_act, SIGNAL(triggered()), this, SLOT(next()));
49
50 previous_act = new QAction(this);
51 connect(previous_act, SIGNAL(triggered()), this, SLOT(previous()));
52
53 add_current_act = new QAction( "Add current media", this);
54 add_current_act->setEnabled(false);
55 connect(add_current_act, SIGNAL(triggered()), SLOT(addCurrentPlaying()));
56
57 retranslateStrings();
58
59 load();
60
61 connect( this, SIGNAL(triggered(QAction *)),
62 this, SLOT(triggered_slot(QAction *)) );
63
64 addAction(edit_act);
65 addAction(add_current_act);
66 //addAction(jump_act);
67 addSeparator();
68
69 populateMenu();
70
71}
72
73Favorites::~Favorites() {
74 /* qDebug("Favorites::~Favorites"); */
75
76 save();
77 delete_children();
78}
79
80void Favorites::delete_children() {
81 for (int n=0; n < child.count(); n++) {
82 if (child[n]) delete child[n];
83 child[n] = 0;
84 }
85 child.clear();
86}
87
88void Favorites::retranslateStrings() {
89 edit_act->setText( tr("&Edit...") );
90 jump_act->setText( tr("&Jump...") );
91 next_act->setText( tr("&Next") );
92 previous_act->setText( tr("&Previous") );
93 add_current_act->setText( tr("&Add current media") );
94}
95
96Favorites * Favorites::createNewObject(QString filename, QWidget * parent) {
97 return new Favorites(filename, parent);
98}
99
100void Favorites::populateMenu() {
101 for (int n = 0; n < f_list.count(); n++) {
102 QString i = QString::number(n+1);
103 QString name = QString("%1 - " + f_list[n].name() ).arg( i.insert( i.size()-1, '&' ), 3, ' ' );
104 if (f_list[n].isSubentry()) {
105 Favorites * new_fav = createNewObject(f_list[n].file(), parent_widget);
106 new_fav->getCurrentMedia(received_file_playing, received_title);
107 connect(this, SIGNAL(sendCurrentMedia(const QString &, const QString &)),
108 new_fav, SLOT(getCurrentMedia(const QString &, const QString &)));
109 /*
110 new_fav->editAct()->setText( editAct()->text() );
111 new_fav->jumpAct()->setText( jumpAct()->text() );
112 new_fav->nextAct()->setText( nextAct()->text() );
113 new_fav->previousAct()->setText( previousAct()->text() );
114 new_fav->addCurrentAct()->setText( addCurrentAct()->text() );
115 */
116
117 child.push_back(new_fav);
118
119 QAction * a = addMenu( new_fav );
120 a->setText( name );
121 a->setIcon( QIcon( f_list[n].icon() ) );
122 } else {
123 QAction * a = addAction( name );
124 a->setData( f_list[n].file() );
125 a->setIcon( QIcon( f_list[n].icon() ) );
126 a->setStatusTip( f_list[n].file() );
127 }
128 }
129}
130
131void Favorites::updateMenu() {
132 // Remove all except the first 2 items
133 while (actions().count() > FIRST_MENU_ENTRY) {
134 QAction * a = actions()[FIRST_MENU_ENTRY];
135 removeAction( a );
136 a->deleteLater();
137 }
138
139 delete_children();
140
141 populateMenu();
142 markCurrent();
143}
144
145void Favorites::triggered_slot(QAction * action) {
146 if (action->data().isValid()) {
147 QString file = action->data().toString();
148 emit activated( file );
149 current_file = file;;
150 markCurrent();
151 }
152}
153
154void Favorites::markCurrent() {
155 for (int n = FIRST_MENU_ENTRY; n < actions().count(); n++) {
156 QAction * a = actions()[n];
157 QString file = a->data().toString();
158 QFont f = a->font();
159
160 if ((!file.isEmpty()) && (file == current_file)) {
161 f.setBold(true);
162 a->setFont( f );
163 } else {
164 f.setBold(false);
165 a->setFont( f );
166 }
167 }
168}
169
170int Favorites::findFile(QString filename) {
171 for (int n = 0; n < f_list.count(); n++) {
172 if (f_list[n].file() == filename) return n;
173 }
174 return -1;
175}
176
177void Favorites::next() {
178 qDebug("Favorites::next");
179
180 int current = findFile(current_file);
181
182 int i = current;
183 if (current < 0) current = 0;
184
185 do {
186 i++;
187 if (i == current) break;
188 if (i >= f_list.count()) i = 0;
189 } while (f_list[i].isSubentry());
190
191 QAction * a = actions()[i+FIRST_MENU_ENTRY]; // Skip "edit" and separator
192 if (a != 0) {
193 a->trigger();
194 }
195}
196
197void Favorites::previous() {
198 qDebug("Favorites::previous");
199
200 int current = findFile(current_file);
201
202 int i = current;
203 if (current < 0) current = 0;
204
205 do {
206 i--;
207 if (i == current) break;
208 if (i < 0) i = f_list.count()-1;
209 } while (f_list[i].isSubentry());
210
211 QAction * a = actions()[i+FIRST_MENU_ENTRY]; // Skip "edit" and separator
212 if (a != 0) {
213 a->trigger();
214 }
215}
216
217void Favorites::getCurrentMedia(const QString & filename, const QString & title) {
218 qDebug("Favorites::getCurrentMedia: '%s', '%s'", filename.toUtf8().constData(), title.toUtf8().constData());
219
220 if (!filename.isEmpty()) {
221 received_file_playing = filename;
222 received_title = title;
223
224 emit sendCurrentMedia(filename, title);
225
226 add_current_act->setEnabled(true);
227 }
228}
229
230void Favorites::addCurrentPlaying() {
231 if (received_file_playing.isEmpty()) {
232 qDebug("Favorites::addCurrentPlaying: received file is empty, doing nothing");
233 } else {
234 Favorite fav;
235 fav.setName(received_title);
236 fav.setFile(received_file_playing);
237 f_list.append(fav);
238 save();
239 updateMenu();
240 }
241}
242
243void Favorites::save() {
244 qDebug("Favorites::save");
245
246 QFile f( _filename );
247 if ( f.open( QIODevice::WriteOnly ) ) {
248 QTextStream stream( &f );
249 stream.setCodec("UTF-8");
250
251 stream << "#EXTM3U" << "\n";
252 for (int n = 0; n < f_list.count(); n++) {
253 stream << "#EXTINF:0,";
254 stream << f_list[n].name() << ",";
255 stream << f_list[n].icon() << ",";
256 stream << f_list[n].isSubentry() << "\n";
257 stream << f_list[n].file() << "\n";
258 }
259 f.close();
260 }
261}
262
263void Favorites::load() {
264 qDebug("Favorites::load");
265
266 QRegExp m3u_id("^#EXTM3U|^#M3U");
267 QRegExp info1("^#EXTINF:(.*),(.*),(.*)");
268 QRegExp info2("^#EXTINF:(.*),(.*),(.*),(.*)");
269
270 QFile f( _filename );
271 if ( f.open( QIODevice::ReadOnly ) ) {
272
273 f_list.clear();
274
275 Favorite fav;
276
277 QTextStream stream( &f );
278 stream.setCodec("UTF-8");
279
280 QString line;
281 while ( !stream.atEnd() ) {
282 line = stream.readLine(); // line of text excluding '\n'
283 //qDebug("info2.indexIn: %d", info2.indexIn(line));
284 //qDebug("info1.indexIn: %d", info1.indexIn(line));
285 //qDebug( " * line: '%s'", line.toUtf8().data() );
286 if (m3u_id.indexIn(line)!=-1) {
287 //#EXTM3U
288 // Ignore line
289 }
290 else
291 if (info2.indexIn(line) != -1) {
292 fav.setName( info2.cap(2) );
293 fav.setIcon( info2.cap(3) );
294 fav.setSubentry( info2.cap(4).toInt() == 1 );
295 }
296 else
297 // Compatibility with old files
298 if (info1.indexIn(line) != -1) {
299 fav.setName( info1.cap(2) );
300 fav.setIcon( info1.cap(3) );
301 fav.setSubentry( false );
302 }
303 else
304 if (line.startsWith("#")) {
305 // Comment
306 // Ignore
307 } else {
308 fav.setFile( line );
309 if (fav.name().isEmpty()) fav.setName(line);
310 //qDebug("Favorites::load: adding '%s' '%s'", fav.name().toUtf8().constData(), fav.file().toUtf8().constData());
311 f_list.append(fav);
312
313 // Clear data
314 fav.setName("");
315 fav.setFile("");
316 fav.setIcon("");
317 fav.setSubentry(false);
318 }
319 }
320 f.close();
321 }
322}
323
324void Favorites::edit() {
325 qDebug("Favorites::edit");
326
327 FavoriteEditor e(parent_widget);
328
329 e.setData(f_list);
330 e.setStorePath( QFileInfo(_filename).absolutePath() );
331
332 if (e.exec() == QDialog::Accepted) {
333 f_list = e.data();
334 save();
335 updateMenu();
336 /*
337 for (int n = 0; n < f_list.count(); n++) {
338 qDebug("item %d: name: '%s' file: '%s'", n, f_list[n].name().toUtf8().constData(), f_list[n].file().toUtf8().constData());
339 }
340 */
341 }
342}
343
344void Favorites::jump() {
345 qDebug("Favorites::jump");
346
347 bool ok;
348 int item = QInputDialog::getInteger(parent_widget, tr("Jump to item"),
349 tr("Enter the number of the item in the list to jump:"),
350 last_item, 1, f_list.count(), 1, &ok);
351 if (ok) {
352 last_item = item;
353 item--;
354 actions()[item+FIRST_MENU_ENTRY]->trigger();
355 }
356}
357
358// Language change stuff
359void Favorites::changeEvent(QEvent *e) {
360 if (e->type() == QEvent::LanguageChange) {
361 retranslateStrings();
362 } else {
363 QWidget::changeEvent(e);
364 }
365}
366
367#include "moc_favorites.cpp"
368
Note: See TracBrowser for help on using the repository browser.