summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/dub/dub/dub.cpp
blob: 5322ae6ec6659f82942928388ff1323c78bcfb50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/***************************************************************************
                          dub.cpp  -  description
                             -------------------
    begin                : Tue Oct 23 01:44:51 EEST 2001
    copyright            : (C) 2001 by Eray Ozkural (exa)
    email                : erayo@cs.bilkent.edu.tr
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// include files for QT
#include <tqdir.h>
#include <tqprinter.h>
#include <tqpainter.h>

// include files for KDE
#include <kiconloader.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <kmenubar.h>
#include <kstatusbar.h>
#include <klocale.h>
#include <kconfig.h>
#include <kstdaction.h>
#include <kurl.h>
#include <kurlrequester.h>
#include <noatun/playlist.h>

#include <vector>
#include <algorithm>
using std::vector;
using std::iterator;

// application specific includes
#include "dub.h"
#include "dub.moc"
#include "dubview.h"
#include "dubplaylist.h"
#include "dubprefs.h"
#include "dubconfigmodule.h"

#include "random.h"
int Random::seed;

#define ID_STATUS_MSG 1

Dub::Dub(DubPlaylist* plist)
  : DubApp(0)
  , playlist(*plist)
  , dubconfig(*plist->dubconfig)
  , activeFile(0)
  , linear_onedir(this)
  , linear_recursive(this)
  , shuffle_onedir(this)
  , shuffle_recursive(this)
{
  connect( view->dirOperator(),
           TQT_SIGNAL(fileSelected(const KFileItem*)),
           this,
           TQT_SLOT(fileSelected(const KFileItem*)) );
  connect( dubconfig.prefs->mediaDirectory,
           TQT_SIGNAL( urlSelected (const TQString &) ),
           this,
           TQT_SLOT( mediaHomeSelected (const TQString &) ) );
  connect( this,
           TQT_SIGNAL(setMediaHome(KURL)),
           view,
           TQT_SLOT(setDir(KURL)) );
  configure_sequencing();
  emit setMediaHome(dubconfig.mediaDirectory);
}

/** File selected */
void Dub::fileSelected(const KFileItem * file) {
  kdDebug(90010) << "dub: file selected " << file << endl;
  activeFile = const_cast<KFileItem *>(file);
  playlist.setCurrent(file, true);
}

void Dub::mediaHomeSelected(const TQString& url) {
  kdDebug(90010) << "media home selected:" << endl;
  emit setMediaHome( KURL(url) );
}

/** changes the active file to the next item
 */
void Dub::selectNextFile() {
  configure_sequencing();
  sequencer->next();
}

/** No descriptions */
KFileItem* Dub::queryRoot() {
  return view->dirLister()->rootItem();
}

/** First file in the directory */
const KFileItem* Dub::queryFirstFile() {
  return sequencer->first();
}

/** Select previous file */
void Dub::selectPreviousFile() {
  configure_sequencing();
  sequencer->prev();
}

void Dub::configure_sequencing()
{
  switch (dubconfig.playMode) {
  case DubConfigModule::allFiles:
    if (dubconfig.playOrder==DubConfigModule::normal) {
      linear_recursive.init(dubconfig.mediaDirectory);
      sequencer = &linear_recursive;
    }
    else if (dubconfig.playOrder==DubConfigModule::shuffle) {
      shuffle_recursive.init(dubconfig.mediaDirectory);
      sequencer = &shuffle_recursive;
    }
    break;
  case DubConfigModule::recursiveDir:
    linear_recursive.init(view->currentDirectory().path());
    sequencer = &linear_recursive;
    break;
  case DubConfigModule::oneDir:
    if (dubconfig.playOrder==DubConfigModule::normal)
      sequencer = &linear_onedir;
    else if (dubconfig.playOrder==DubConfigModule::shuffle) {
      shuffle_onedir.init(view->currentDirectory().path());
      sequencer = &shuffle_onedir;
    }
    break;
  }
}

void Dub::Sequencer::set_file(KFileItem** file, KFileItem* val) {
  assert(val);
  if (*file)
    delete *file;
  *file = new KFileItem(*val);
  kdDebug(90010) << "set_file to " << val->url() << endl;
}

KFileItem* Dub::Linear_Seq::first(TQPtrList<KFileItem> & items)
{
  // find first file
  KFileItem* firstFile = 0;
  for (KFileItem* item = items.first(); item; item = items.next() ) {
    if (item->isFile()) {
      firstFile = item;
      break;
    }
  }
  return firstFile;
}

KFileItem* Dub::Linear_Seq::last(TQPtrList<KFileItem> & items)
{
  // find last file
  KFileItem* lastFile = 0;
  for (KFileItem* item = items.last(); item; item = items.prev() ) {
    if (item->isFile()) {
      lastFile = item;
      break;
    }
  }
  return lastFile;
}

bool Dub::Linear_Seq::find(TQPtrList<KFileItem> & items, KFileItem* a_file)
{
  // find file
   for (KFileItem *file=items.first(); file; file=items.next() )
      if (file->isFile() && file->cmp(*a_file)) {
        kdDebug(90010) << " found " << (file->url()) << endl;
        return true;
      }
  return false;
}

KFileItem* Dub::Linear_Seq::next(TQPtrList<KFileItem> & items,
			      KFileItem** active_file)
{
  KFileItem* ret = 0;
  assert(active_file);
  bool found = false;
  if (*active_file) {
    if (find(items, *active_file)) {
      KFileItem* next = items.next();
      for (; next && !next->isFile(); next = items.next()) ; // find next file
      if (next && next->isFile())
	set_file(active_file, next);
        found = true;
        ret = next;
    }
  }
  if (!found) { // try to get the first one then
     KFileItem *fst = first(items);
     if (fst) {
       set_file(active_file, fst);
       ret = fst;
     }
   }
  return ret;
}

KFileItem* Dub::Linear_Seq::prev(TQPtrList<KFileItem> & items,
			      KFileItem** active_file)
{
  KFileItem* ret = 0;
  assert(active_file);
  bool found = false;
  if (*active_file) {
    // locate current item
    if (find(items, *active_file)) {
      KFileItem* prev = items.prev();
      for (; prev && !prev->isFile(); prev = items.prev()) ; // find prev file
      if (prev && prev->isFile()) {
        set_file(active_file, prev);
        found = true;
        ret = prev;
      }
    }
  }
  if (!found) { // try to get the last one then
     KFileItem *lst = last(items);
     if (lst) {
       set_file(active_file, lst);
       ret = lst;
     }
   }
  return ret;
}

KFileItem* Dub::Linear_OneDir::first()
{
  KFileItem* first = Linear_Seq::first(dub.view->items());
  if (first)
    set_file(&first_file, first);
  else {
    if (first_file) {		// invalidate first
      delete first_file;
      first_file = 0;
    }
  }
  return first_file;
}

//KFileItem* Dub::Linear_OneDir::getAfter(KFileItem* item)
//{

//}

void Dub::Linear_OneDir::next()
{
  KFileItem *f = Linear_Seq::next(dub.view->items(), &dub.activeFile);
  if (f) {
    dub.view->selectFile(f);
  }
}

void Dub::Linear_OneDir::prev()
{
  KFileItem *f = Linear_Seq::prev(dub.view->items(), &dub.activeFile);
  if (f) {
    dub.view->selectFile(f);
  }
}

Dub::Dir_Node::Dir_Node(TQString d, bool forward)
  : dir(d), past_begin(false)
{
  kdDebug(90010) << "cons dir node " << d << endl;
  // process entry list, form a list of subdirs and normal files
  file_items.setAutoDelete(true);
  TQDir dir_obj(dir);
  TQFileInfoList* entries =
    const_cast<TQFileInfoList*>(dir_obj.entryInfoList());
  for ( TQFileInfo *file = entries->first(); file; file = entries->next() ) {
    if (file->isDir() && file->absFilePath().length()>d.length()) {
      kdDebug(90010) << "dub: dir " << file->absFilePath() << endl;
      subdirs.append(file->absFilePath());
    }
    if (file->isFile()) {
      // price for portability
      kdDebug(90010) << "dub: file " << file->absFilePath() << endl;
      KFileItem* item = new KFileItem(KFileItem::Unknown, KFileItem::Unknown,
				      file->absFilePath(), true);
      file_items.append(item);
    }
  } // for

  init_traversal(forward);

  kdDebug(90010) << "dir node cons end" << endl;
}

void Dub::Dir_Node::init_traversal(bool forward)
{
  kdDebug(90010) << "init traversal" << endl;
  // initialize traversal information
  if (forward) {
    current_subdir = subdirs.begin();
    file_items.first();
  }
  else {
    current_subdir = subdirs.end();
    if (current_subdir!=subdirs.begin())
      current_subdir--; // last item
    else
      past_begin=true;
    file_items.last();
  }
  current_file = file_items.current();
  kdDebug(90010) << "current subdir " << *current_subdir << endl;
  kdDebug(90010) << "current file " << current_file << endl;
}

Dub::Recursive_Seq::Recursive_Seq()
{
  play_stack.setAutoDelete(true);
}

void Dub::Recursive_Seq::init(const KURL & root)
{
  TQString new_root = canonical_path(root.path());
  if (recursion_root != new_root) {
    // change recursion stack
    recursion_root = new_root;
    kdDebug(90010) << "rec: new root is " << recursion_root << endl;
    play_stack.clear();
    push_dir(recursion_root); // start pre-order traversal
  }
}

// get canonical path, we need this
TQString Dub::Recursive_Seq::canonical_path(TQString dir)
{
//   kdDebug(90010) << "canonical_path " << dir << endl;
  //assert(dir.isLocalFile());
  TQDir path(dir);
  return path.canonicalPath();
}

// check if dir is contained in the stack
bool Dub::Recursive_Seq::check_dir(TQString dir)
{
  kdDebug(90010) << "check_dir " << dir << endl;
  bool found = false;
  for ( Dir_Node *cur_dir = play_stack.first();
        !found && cur_dir; cur_dir = play_stack.next() ) {
    if (cur_dir->dir==dir)
      found = true;
  }
  return found;
}

bool Dub::Recursive_Seq::push_dir(TQString dir, bool forward)
{
  kdDebug(90010) << "push_dir " << dir << ", forward?" << forward << endl;
  TQString cpath = canonical_path(dir);
  if (check_dir(cpath))		// is it in stack?
    return false;		// avoid infinite recursion
  else {
    Dir_Node* node = new Dir_Node(cpath, forward);
    play_stack.append(node);
    kdDebug(90010) << "stack after push:" << endl;
    print_stack();
    return true;
  }
}

bool Dub::Recursive_Seq::pop_dir()
{
  assert(!play_stack.isEmpty());
  kdDebug(90010) << "pop_dir" << endl;
  play_stack.removeLast();
  return !play_stack.isEmpty();
}

bool Dub::Recursive_Seq::advance(bool forward)
{
  Dir_Node* top = play_stack.getLast();
  kdDebug(90010) << "first child " << top->subdirs.first() << endl;
  kdDebug(90010) << "current child " << *top->current_subdir << endl;
  kdDebug(90010) << "last child " << top->subdirs.last() << endl;
  if (forward) {
    top->current_subdir++;	// advance dir
    return top->current_subdir!=top->subdirs.end();
  }
  else
    if (top->current_subdir!=top->subdirs.begin()) {
      top->current_subdir--;
      return true;
    }
    else {
      top->past_begin=true;
      return false;
    }
}

void Dub::Recursive_Seq::pop_preorder(bool forward)
{
  if (pop_dir()) {		// pop visited
    advance(forward);		// advance to next node
    Dir_Node* top = play_stack.getLast();
    kdDebug(90010) << "new child " << *top->current_subdir << endl;
    if (forward)
      next_preorder();		// continue processing
    else
      prev_preorder();		// continue processing
  }
  else {
    kdDebug(90010) << "push root" << endl;
    push_dir(recursion_root, forward);	// back to the beginning if at end
  }
}

void Dub::Recursive_Seq::next_preorder()
{
  assert(!play_stack.isEmpty());	// recursion stack cannot be empty
  kdDebug(90010) << "next_preorder, stack:" << endl;
  print_stack();
  Dir_Node* top = play_stack.getLast();
  if (top->subdirs.isEmpty() || top->current_subdir==top->subdirs.end()) {
    kdDebug(90010) << "rec: subtrees done" << endl;
    pop_preorder(true);    // pop if subtrees done
  }
  else {
    TQString subdir = *top->current_subdir; // we have a subdir
    push_dir(subdir, true); // push directory w/ forward iterators
  }
}

void Dub::Recursive_Seq::prev_preorder()
{
  assert(!play_stack.isEmpty());	// recursion stack cannot be empty
  kdDebug(90010) << "prev_preorder, stack:" << endl;
  print_stack();
  Dir_Node* top = play_stack.getLast();
  if (top->subdirs.isEmpty() || top->past_begin) { // subtrees done?
    kdDebug(90010) << "rec: subtrees done" << endl;
    pop_preorder(false);
  }
  else {
    TQString subdir = *top->current_subdir;
    kdDebug(90010) << "we have children, pushing now " << subdir << endl;
    push_dir(subdir, false); // push directory w/ backward iterators
  }
}

void Dub::Recursive_Seq::print_stack() {
  for ( Dir_Node *cur_dir = play_stack.first();
	cur_dir; cur_dir = play_stack.next() ) {
    kdDebug(90010) <<  cur_dir->dir << endl;
  }
}

Dub::Linear_Recursive::Linear_Recursive(Dub* d)
  : Sequencer(d) {
  kdDebug(90010) << "cons linear/recursive" << endl;
}

KFileItem* Dub::Linear_Recursive::first()
{
  KFileItem* first = bottom_dir()->file_items.getFirst();
  return first;
}

void Dub::Linear_Recursive::next()
{
  assert(!play_stack.isEmpty());
  Dir_Node* top = top_dir();
  TQString dir = top->dir;
  top->current_file = top->file_items.next();
  kdDebug(90010) << "dub current dir: " << dir << endl;
  kdDebug(90010) << "dub current file: " << top->current_file << endl;
  bool cycle = false;
  while (!top_dir()->current_file && !cycle) {
    next_preorder();  // traverse until a non-empty dir or cycle
   if (top_dir()->dir==dir) {
     kdDebug(90010) << "we got a cycle" << endl;
     cycle = true;
     top_dir()->init_traversal(true);
   }
  }
  top = play_stack.getLast();
  kdDebug(90010) << "dub new dir: " << *top->current_subdir << endl;
  kdDebug(90010) << "dub new file: " << top->current_file << endl;
  if (top->current_file) {
    kdDebug(90010) << "dub new file: " << top->current_file->url() << endl;
    dub.activeFile = top->current_file;
    dub.fileSelected(dub.activeFile);
  }
}

void Dub::Linear_Recursive::prev()
{
  assert(!play_stack.isEmpty());
  Dir_Node* top = top_dir();
  TQString dir = top->dir;
  top->current_file = top->file_items.prev();
  kdDebug(90010) << "dub current dir: " << dir << endl;
  kdDebug(90010) << "dub current file: " << top->current_file << endl;
  bool cycle = false;
  while (!top_dir()->current_file && !cycle) {
   prev_preorder();  // traverse until a non-empty dir or cycle
   if (top_dir()->dir==dir) {
     kdDebug(90010) << "we got a cycle" << endl;
     cycle = true;
     top_dir()->init_traversal(false);
   }
  }
  top = play_stack.getLast();
  kdDebug(90010) << "dub new dir: " << *top->current_subdir << endl;
  kdDebug(90010) << "dub new file: " << top->current_file << endl;
  if (top->current_file) {
    kdDebug(90010) << "dub new file: " << top->current_file->url() << endl;
    dub.activeFile = top->current_file;
    dub.fileSelected(dub.activeFile);
  }
}

void Dub::Shuffle_OneDir::init(const TQString& dir)
{
  if (shuffle_dir != dir) {
    kdDebug(90010) << "shuffle/onedir init" << endl;
    shuffle_dir = dir;
    play_index = 0;

    // make a deep copy
    items.clear();
    TQPtrList<KFileItem> & view_items = dub.view->items(); //
    for (KFileItem *file=view_items.first(); file; file=view_items.next() )
      if (file->isFile())	// add only files
	items.append(new KFileItem(*file));

    int num_items = items.count();
    play_order.resize(num_items);
    if (num_items) {		// generate shuffled order
      kdDebug(90010) << num_items << " file items" << endl;
      for (int i=0; i<num_items; i++)
	play_order[i] = i;
      Random random;
      Random::init();
      std::random_shuffle(play_order.begin(), play_order.end(), random);
    }
  }
}

KFileItem* Dub::Shuffle_OneDir::first()
{
  return 0;
}

void Dub::Shuffle_OneDir::next()
{
  kdDebug(90010) << "shuffle/onedir next" << endl;
  if (!items.isEmpty()) {
    play_index = ++play_index % play_order.size();
    dub.activeFile = items.at(play_order[play_index]);
    if (dub.activeFile)
      dub.fileSelected(dub.activeFile);
  }
}

void Dub::Shuffle_OneDir::prev()
{
  kdDebug(90010) << "shuffle/onedir prev" << endl;
  if (!items.isEmpty()) {
    play_index = --play_index % play_order.size();
    dub.activeFile = items.at(play_order[play_index]);
    if (dub.activeFile)
      dub.fileSelected(dub.activeFile);
  }
}

KFileItem* Dub::Shuffle_Recursive::random_file()
{
  assert(!play_stack.isEmpty());
  play_stack.clear();
  push_dir(recursion_root); // start pre-order traversal
  KFileItem* selected = 0;
  double file_probability = 0.3;
  Random::init();
  while (!top_dir()->subdirs.isEmpty() && !selected) {
    if (top_dir()->file_items.isEmpty()) {
      int ix = Random::random_int(top_dir()->subdirs.count());
      push_dir(top_dir()->subdirs[ix]);
    }
    else {
      if (Random::random_double(1.0)<file_probability) {
	int ix = Random::random_int(top_dir()->file_items.count());
	selected = top_dir()->file_items.at(ix);
      }
      else {
	int ix = Random::random_int(top_dir()->subdirs.count());
	push_dir(top_dir()->subdirs[ix]);
      }
    }
  }
  if (!selected) {
    if (!top_dir()->file_items.isEmpty()) {
      int ix = Random::random_int(top_dir()->file_items.count());
      selected = top_dir()->file_items.at(ix);
    }
  }
  return selected;
}

KFileItem* Dub::Shuffle_Recursive::first()
{
  return random_file();
}

void Dub::Shuffle_Recursive::next()
{
  KFileItem* file = random_file();
  if (file) {
    kdDebug(90010) << "shuffle/rec: new file: " << file->url() << endl;
    dub.activeFile = file;
    dub.fileSelected(file);
  }
}

void Dub::Shuffle_Recursive::prev()
{
  KFileItem* file = random_file();
  if (file) {
    kdDebug(90010) << "shuffle/rec: new file: " << file->url() << endl;
    dub.activeFile = file;
    dub.fileSelected(file);
  }
}