summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/lyrics/cmodule.cpp
blob: 6ebe81f90e40d21f2ae3a0b60059e57980028d5d (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
#include "cmodule.h"
#include "lyrics.h"
#include <tqlayout.h>
#include <tqstringlist.h>
#include <tqlabel.h>
#include <tdemessagebox.h>
#include <tqvgroupbox.h>
#include <tqgrid.h>
#include <tdelistbox.h>
#include <klineedit.h>
#include <kbuttonbox.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <kdebug.h>

extern Lyrics *lyrics;

const char *const DEFAULT_NAME =
  "Google,"
  "Pure Lyrics,"
  "Sing365,"
  "Lyrics Planet,"
  "Lyrics World,"
  "Get Lyrics,"
  "AZLyrics,"
  "Astraweb,"
  "SongMeanings,"
  "Google (Feeling Lucky),"
  "Everything2,"
  "Everything2 (author info)";

const char *const DEFAULT_QUERY =
  "http://www.google.com/search?q=lyrics+$(title)+$(author)+$(album),"
  "http://www.purelyrics.com/index.php?search_artist=$(author)&search_album=$(album)&search_title=$(title)&search_lyrics=&search_advsubmit2=Search,"
  "http://search.sing365.com/search.php?searchstr=$(title)&submit=search&category=song,"
  "http://www.lyricsplanet.com/index.php3?style=searchtitle&fix=1&searchstring=$(title),"
  "http://www.lyricsworld.com/cgi-bin/search.cgi?q=$(title)+$(author),"
  "http://www.getlyrics.com/search.php?Song=$(title),"
  "http://www.azlyrics.com/cgi-bin/s.cgi?q=$(title)+$(author),"
  "http://search.lyrics.astraweb.com?word=$(title)+$(author)+$(album),"
  "http://www.songmeanings.net/search.php?type=titles&query=$(title),"
  "http://www.google.com/search?q=lyrics+%22$(title)%22+%22$(author)%22+%22$(album)%22&btnI=I%27m+Feeling+Lucky,"
  "http://everything2.com/index.pl?node=$(title),"
  "http://everything2.com/index.pl?node=$(author),"
  "http://www.letssingit.com/cgi-exe/am.cgi?a=search&p=1&s=$(title)&l=song";

LyricsCModule::LyricsCModule(TQObject *_parent) : CModule(i18n("Lyrics"), i18n("Configure Lyrics Plugin"), "text-x-generic", _parent) {
  /* Thanks to the kde-usability guys for the help designing this dialog!
   * help to simon edwards of KGuardGod, for a big help designing it */
  TQVBoxLayout *vlayout = new TQVBoxLayout(this, KDialog::marginHint(), KDialog::spacingHint());
  TQHBoxLayout *hlayout = new TQHBoxLayout(vlayout, KDialog::spacingHint());
  vlayout->setStretchFactor( hlayout, 1 );

  // Search box
  TQVBoxLayout *boxlayout = new TQVBoxLayout( hlayout, KDialog::spacingHint() );
  boxlayout->addWidget( new TQLabel( i18n("Search providers:" ), this ) );
  providersBox = new TDEListBox( this, "providersBox" );
  boxlayout->addWidget(providersBox);

  boxButtons = new KButtonBox( this,TQt::Vertical );
  boxButtons->addButton( i18n( "New Search Provider" ), this, TQ_SLOT( newSearch() ) );
  boxButtons->addButton( i18n( "Delete Search Provider" ), this, TQ_SLOT( delSearch() ) );
  boxButtons->addButton( i18n( "Move Up" ), this, TQ_SLOT( moveUpSearch() ) );
  boxButtons->addButton( i18n( "Move Down" ), this, TQ_SLOT( moveDownSearch() ) );
  boxButtons->layout();
  boxlayout->addWidget( boxButtons );

  // Edit box
  TQGroupBox *propBox = new TQVGroupBox( i18n("Search Provider Properties" ), this );
  TQGrid *editGrid = new TQGrid(2, propBox );
  editGrid->setSpacing(  propBox->insideSpacing() );
  new TQLabel( i18n( "Name:" ), editGrid );
  nameEdit = new KLineEdit( editGrid );
  new TQLabel( i18n( "Query:" ), editGrid );
  queryEdit = new KLineEdit( editGrid );
  /* ATTENTION to translators:
   * The property names can't be translated. This means that $(author) must be kept as $(author), $(title) as $(title), etc, or it won't work.*/
  TQLabel *textLabel = new TQLabel(i18n("For your query, you can use any property of your multimedia item, just enclosing it with a $(property).\n\nSome common properties used are $(title), $(author) and $(album). For example, to search in Google for the author, title and track, just use:\nhttp://www.google.com/search?q=$(author)+$(title)+$(track)"), propBox, "textLabel");
  textLabel->setAlignment(TQt::WordBreak);
  hlayout->addWidget( propBox, 1 );

  /* Signal/slots */
  nameEdit->setEnabled( false );
  queryEdit->setEnabled( false );
  connect( providersBox, TQ_SIGNAL( highlighted( TQListBoxItem * ) ), this, TQ_SLOT( selected( TQListBoxItem * ) ) );
  connect( nameEdit, TQ_SIGNAL( textChanged( const TQString &) ), this, TQ_SLOT( nameChanged( const TQString & ) ) );
  connect( queryEdit, TQ_SIGNAL( textChanged( const TQString & ) ), this, TQ_SLOT( queryChanged( const TQString & ) ) );



  vlayout->addStretch();
  reopen();
  save();
}

void LyricsCModule::newSearch(TQString name, TQString query) {
  kdDebug(90020) << "New search" << endl;
  SearchProvider s = { name, query };
  mProviders.push_back( s );
  providersBox->insertItem( name );
  providersBox->setCurrentItem( providersBox->count()-1 );
  nameEdit->setEnabled( true );
  queryEdit->setEnabled( true );
//X   if ( providersBox->count() == 1 )
//X     providersBox->setCurrentItem( 0 );
}

void LyricsCModule::selected( TQListBoxItem *i ) {
  kdDebug(90020) << "selected" << endl;
  int index = providersBox->index( i );
  if ( index < 0 )
    return;
  if ( nameEdit->text() != mProviders[ index ].name )
    nameEdit->setText( mProviders[ index ].name );
  if ( queryEdit->text() != mProviders[ index ].url )
    queryEdit->setText( mProviders[ index ].url );
}


void LyricsCModule::delSearch() {
  if ( mProviders.size() == 1 ) {
    KMessageBox::sorry( this, i18n( "You must have at least one search provider. The current one will not be removed." ) );
    return;
  }
  int index = providersBox->currentItem();
  TQValueVector<SearchProvider>::iterator it;
  for ( it = mProviders.begin(); ( *it ).name != mProviders[ index ].name || ( *it ).url != mProviders[ index ].url; ++it );
  mProviders.erase( it );
  providersBox->removeItem( index );
  providersBox->setSelected( providersBox->currentItem(), true );
}

void LyricsCModule::moveUpSearch() {
  if ( providersBox->currentItem() <= 0 )
    return;
  int index = providersBox->currentItem();
  TQString name = mProviders[ index ].name;
  TQString url = mProviders[ index ].url;
  mProviders[ index ].name = mProviders[ index-1 ].name;
  mProviders[ index ].url = mProviders[ index-1 ].url;
  mProviders[ index-1 ].name = name;
  mProviders[ index-1 ].url = url;
  providersBox->changeItem( mProviders[ index-1 ].name, index-1 );
  providersBox->changeItem( mProviders[ index ].name, index );
  providersBox->setSelected( index-1, true );
}

void LyricsCModule::moveDownSearch() {
  if ( static_cast<unsigned int>( providersBox->currentItem() ) >= providersBox->count()-1 )
    return;
  int index = providersBox->currentItem();
  TQString name = mProviders[ index ].name;
  TQString url = mProviders[ index ].url;
  mProviders[ index ].name = mProviders[ index+1 ].name;
  mProviders[ index ].url = mProviders[ index+1 ].url;
  mProviders[ index+1 ].name = name;
  mProviders[ index+1 ].url = url;
  providersBox->changeItem( mProviders[ index+1 ].name, index+1 );
  providersBox->changeItem( mProviders[ index ].name, index );
  providersBox->setSelected( index+1, true );
}

void LyricsCModule::nameChanged( const TQString &name ) {
  kdDebug(90020) << "name changed" << endl;
  if ( providersBox->currentItem() < 0 )
    return;
  mProviders[ providersBox->currentItem() ].name = name;
  if ( name != providersBox->text( providersBox->currentItem() ) )
    providersBox->changeItem( name, providersBox->currentItem() );
}

void LyricsCModule::queryChanged( const TQString &query ) {
  kdDebug(90020) << "query changed" << endl;
  if ( providersBox->currentItem() < 0 )
    return;
  mProviders[ providersBox->currentItem() ].url = query;
}


void LyricsCModule::save() {
  TDEConfig *conf = TDEGlobal::config();
  conf->setGroup( "Lyrics" );
  TQStringList queryList, nameList;
  TQValueVector<SearchProvider>::iterator it;
  for ( it = mProviders.begin(); it != mProviders.end(); ++it ) {
    kdDebug(90020) << "query:" << ( *it ).url << endl;
    queryList += ( *it ).url;
    nameList += ( *it ).name;
  }
  conf->writeEntry( "queryList", queryList );
  conf->writeEntry( "nameList", nameList );
  /* TODO */
  // APPLY settings
  if ( lyrics )
    lyrics->setProviders( mProviders );
}

void LyricsCModule::reopen() {
  TQStringList queryList, nameList;
  TDEConfig *conf = TDEGlobal::config();
  mProviders.clear();
  providersBox->clear();
  kdDebug(90020) << "config read" << endl;
  conf->setGroup( "Lyrics" );
  queryList = conf->readListEntry( "queryList" );
  nameList = conf->readListEntry( "nameList" );
  if ( queryList.count() == 0 && nameList.count() == 0 ) {
    queryList = TQStringList::split( ",", DEFAULT_QUERY );
    nameList = TQStringList::split( ",", DEFAULT_NAME );
  }
  TQStringList::Iterator queryIt, nameIt;
  for ( queryIt = queryList.begin(), nameIt = nameList.begin(); queryIt != queryList.end() && nameIt != nameList.end(); ++queryIt, ++nameIt ) {
    kdDebug(90020) << "Read:" << *queryIt << " and " << *nameIt << endl;
    newSearch( *nameIt, *queryIt );
  }
}

#include "cmodule.moc"