Module: WikiPluginAdapter

Includes:
PluginAdapter
Defined in:
lib/wiki_lyrics/cli/wikipluginadapter.rb,
lib/wiki_lyrics/amarok/wikipluginadapter.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

@@MENU_ENTRY_ALBUM =
I18n.get( "amarok.wikiplugin.checkalbum" )
@@MENU_ENTRY_SONG =
I18n.get( "amarok.wikiplugin.checksong" )
I18n.get( "amarok.wikiplugin.uploadcover" )
I18n.get( "amarok.wikiplugin.submitcontent" )

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PluginAdapter

#add_custom_checkeable_menu_item, #add_custom_menu_item, #notify, #plugin_name, #popup, #remove_custom_checkeable_menu_item, #remove_custom_menu_item

Class Method Details

.included(including) ⇒ Object

Hack to make module methods become class methods when the module gets included



31
32
33
34
35
36
37
# File 'lib/wiki_lyrics/cli/wikipluginadapter.rb', line 31

def WikiPluginAdapter.included( including )
  if including.is_a?( Class )
    including.extend( ClassMethods ) # adds class methods
  else # including.is_a?( Module )
    including::ClassMethods.append_class_methods( self )
  end
end

Instance Method Details

#check_album_page(file) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 231

def check_album_page( file )
  album_data = Amarok.query_album_data( file )
  if album_data.empty? || album_data["tracks"].empty? # can't even attempt to search the album...
    notify( I18n.get( "amarok.wikiplugin.checkalbum.noinfofound" ) )
  else
    begin
      tracks = []
      album_data["tracks"].each() do |track|
        tracks << MediaWikiLyrics::TrackData.new( track["artist"], track["title"], track["length"] )
      end
      submit_album_page(
        MediaWikiLyrics::AlbumData.new( tracks, album_data["album"], album_data["year"] ),
        album_data["image_path"],
        false, # is user allowed to overwrite existing pages?
        false, # do we know the page doesn't exists?
        @wpa_review, # should we show the review contents dialog?
        false # should user be forced to review content? (true causes the operation to abort if he doesn't)
      )
    rescue TimeoutError
      notify( I18n.get( "amarok.application.search.plugintimeout", plugin_name(), site_host() ) )
    end
  end
end

#check_song_page(file) ⇒ Object



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
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 338

def check_song_page( file )

  song_data = Amarok.query_song_data( file )

  if song_data.empty?
    notify( I18n.get( "amarok.wikiplugin.checksong.noinfofound" ) )
    return
  end

  request = Lyrics::Request.new( song_data["artist"], song_data["title"], song_data["album"], song_data["year"] )

  notify( I18n.get( "wiki.submitsong.searchingpage", request.title, request.artist ) )

  response = self.lyrics_full_search( request )
  response_plugin = self

  if response.lyrics
    if ! response.custom_data["reviewed"]
      notify( I18n.get( "wiki.submitsong.pagefound.autogenerated", request.title, request.artist ) )
      return if ! @wpa_prompt_autogen # autogen lyrics found but user won't review them, nothing to do for us
    else
      notify( I18n.get( "wiki.submitsong.pagefound", request.title, request.artist ) )
      return # reviewed lyrics found, nothing to do for us
    end
  else
    notify( I18n.get( "wiki.submitsong.nopagefound", request.title, request.artist ) )
  end

  if ! response.lyrics
    # if the lyrics weren't found on the site, first we try to find them in Amarok database
    response.lyrics = Amarok.query_song_lyrics( file )
    response_plugin = nil
    # if we didn't found them there, we try the other selected sites
    if ! response.lyrics
      used_plugins = Plugins.used_plugins().clone()
      used_plugins.delete( self )
      used_plugins.each() do |plugin|
        begin
          response = plugin.lyrics_full_search( request )
          if request.lyrics
            response_plugin = plugin
            break
          end
        rescue TimeoutError
          notify( I18n.get("amarok.application.search.plugintimeout", plugin.plugin_name(), plugin.site_host()) )
        end
      end
    end
  end

  wiki_process_response( response, response_plugin, true )

end

#check_submit_conditionsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wiki_lyrics/cli/wikipluginadapter.rb', line 53

def check_submit_conditions()
    if @wpa_submit
    ( @wpa_username, @wpa_password, false )
  else
    logout()
  end

  if ! logged_in?
    @wpa_submit = false
  end

  if ! @wpa_submit || ! @wpa_review
    @wpa_prompt_no_lyrics = false
    @wpa_prompt_autogen = false
  end
end

#configureObject



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
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 140

def configure()

  values = {
    "site_name"     => site_name(),
    "review"      => @wpa_review,
    "submit"      => @wpa_submit,
    "prompt_autogen"  => @wpa_prompt_autogen,
    "prompt_no_lyrics"  => @wpa_prompt_no_lyrics,
    "username"      => @wpa_username,
    "password"      => @wpa_password
  }

  if GUI.show_wiki_plugin_dialog( values )
    @wpa_review = values["review"].to_s() != "false"
    @wpa_submit = values["submit"].to_s() == "true"
    @wpa_prompt_autogen = values["prompt_autogen"].to_s() == "true"
    @wpa_prompt_no_lyrics = values["prompt_no_lyrics"].to_s() == "true"
    @wpa_username = values["username"]
    @wpa_password = values["password"]
  end

  check_submit_conditions()

  if @wpa_submit
    add_custom_menu_item( @@MENU_UPLOAD_COVER )
    add_custom_menu_item( @@MENU_ENTRY_ALBUM )
    add_custom_menu_item( @@MENU_ENTRY_SONG )
  else
    remove_custom_menu_item( @@MENU_UPLOAD_COVER )
    remove_custom_menu_item( @@MENU_ENTRY_ALBUM )
    remove_custom_menu_item( @@MENU_ENTRY_SONG )
  end

  add_custom_checkeable_menu_item( @@MENU_SUBMIT_CONTENT, @wpa_submit )

end

#on_custom_menu_item_selected(menu, item, urls) ⇒ Object



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
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 193

def on_custom_menu_item_selected( menu, item, urls )
  return if menu != plugin_name()
  if item == @@MENU_UPLOAD_COVER
    url = URI.parse( urls[0] )
    upload_album_cover( url.scheme == "file" ? URI.decode( url.path ) : nil)
  elsif item == @@MENU_ENTRY_ALBUM
    url = URI.parse( urls[0] )
    return if url.scheme != "file"
    check_album_page( URI.decode( url.path ) )
  elsif item == @@MENU_ENTRY_SONG
    urls.each() do |url|
      # TODO add skip dialog, show remaining
      url = URI.parse( url )
      return if url.scheme != "file"
      check_song_page( URI.decode( url.path ) )
    end
  elsif /\] #{@@MENU_SUBMIT_CONTENT}$/.match( item )
    if ! @wpa_submit && Strings.empty?( @wpa_username ) # there's not username to login with
      @wpa_submit = true
      configure()
      return
    end
    @wpa_submit = ! @wpa_submit
    check_submit_conditions()
    if @wpa_submit
      add_custom_menu_item( @@MENU_UPLOAD_COVER )
      add_custom_menu_item( @@MENU_ENTRY_ALBUM )
      add_custom_menu_item( @@MENU_ENTRY_SONG )
    else
      remove_custom_menu_item( @@MENU_UPLOAD_COVER )
      remove_custom_menu_item( @@MENU_ENTRY_ALBUM )
      remove_custom_menu_item( @@MENU_ENTRY_SONG )
    end
    add_custom_checkeable_menu_item( @@MENU_SUBMIT_CONTENT, @wpa_submit )
  end

end

#on_quitObject



186
187
188
189
190
191
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 186

def on_quit()
  remove_custom_menu_item( @@MENU_UPLOAD_COVER )
  remove_custom_menu_item( @@MENU_ENTRY_ALBUM )
  remove_custom_menu_item( @@MENU_ENTRY_SONG )
  remove_custom_checkeable_menu_item( @@MENU_SUBMIT_CONTENT )
end

#on_startObject



177
178
179
180
181
182
183
184
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 177

def on_start()
  if @wpa_submit
    add_custom_menu_item( @@MENU_UPLOAD_COVER )
    add_custom_menu_item( @@MENU_ENTRY_ALBUM )
    add_custom_menu_item( @@MENU_ENTRY_SONG )
  end
  add_custom_checkeable_menu_item( @@MENU_SUBMIT_CONTENT, @wpa_submit )
end

#read_config(config_file) ⇒ Object



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
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 79

def read_config( config_file )

  values = {
    "#{plugin_name}_submit"       => nil,
    "#{plugin_name}_review"       => nil,
    "#{plugin_name}_prompt_autogen"   => nil,
    "#{plugin_name}_prompt_no_lyrics" => nil,
    "#{plugin_name}_username"     => nil,
    "#{plugin_name}_password"     => nil,
    "#{plugin_name}_cookie"       => nil,
    "#{plugin_name}_last_auth_check"  => nil,
    "#{plugin_name}_authorized"     => nil,
  }

  XMLHash.read( config_file, values )

  @wpa_review = values["#{plugin_name}_review"] != "false"
  @wpa_submit = values["#{plugin_name}_submit"] == "true"
  @wpa_prompt_autogen = values["#{plugin_name}_prompt_autogen"] == "true"
  @wpa_prompt_no_lyrics = values["#{plugin_name}_prompt_no_lyrics"] == "true"
  @wpa_username = Strings.descramble( values["#{plugin_name}_username"] )
  @wpa_password = Strings.descramble( values["#{plugin_name}_password"] )

    if @wpa_submit
    restore_session_params(
      @wpa_username,
      @wpa_password,
      values["#{plugin_name}_cookie"],
      values["#{plugin_name}_last_auth_check"].to_i(),
      values["#{plugin_name}_authorized"] == "true"
    )
  end

  check_submit_conditions()

end

#set_submit_settings(username, password, review, prompt_autogen, prompt_no_lyrics) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wiki_lyrics/cli/wikipluginadapter.rb', line 70

def set_submit_settings( username, password, review, prompt_autogen, prompt_no_lyrics )
  @wpa_submit = true
  @wpa_review = review
  @wpa_prompt_autogen = prompt_autogen
  @wpa_prompt_no_lyrics = prompt_no_lyrics
  @wpa_username = username
  @wpa_password = password
  if ! @wpa_submit || ! @wpa_review
    @wpa_prompt_no_lyrics = false
    @wpa_prompt_autogen = false
  end
end

#submit?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/wiki_lyrics/cli/wikipluginadapter.rb', line 49

def submit?()
  return @wpa_submit
end

#upload_album_cover(file) ⇒ Object



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
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 256

def upload_album_cover( file )

  album_data = Amarok.query_album_data( file )
  if album_data.empty?
    album_data["artist"] = album_data["album"] = album_data["year"] = album_data["image_path"] = ""
  end
  album_data["site_name"] = site_name()

  if GUI.show_upload_cover_dialog( album_data )

    album_data["year"] = album_data["year"].to_s()
    if Strings.empty?( album_data["artist"] ) || Strings.empty?( album_data["album"] ) || Strings.empty?( album_data["year"] )
      notify( I18n.get( "amarok.wikiplugin.uploadcover.invalidalbumparams" ) )
    elsif Strings.empty?( album_data["image_path"] )
      notify( I18n.get( "amarok.wikiplugin.uploadcover.noimagepath" ) )
    else
      notify( I18n.get( "amarok.wikiplugin.uploadcover.searching", album_data["album"], album_data["artist"] ) )
      begin
        if ! find_album_art_name( album_data["artist"], album_data["album"], album_data["year"] )
          upload_cover_image( album_data["image_path"], album_data["artist"], album_data["album"], album_data["year"])
        else
          notify( I18n.get( "amarok.wikiplugin.uploadcover.found" ) )
        end
      rescue TimeoutError
        notify( I18n.get( "amarok.application.search.plugintimeout", plugin_name(), site_host() ) )
      end
    end
  end

end

#wiki_process_response(response, response_plugin, wiki_searched) ⇒ Object

returns true if the review contents page is shown to the user (which can only happen when there was something to submit)



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
# File 'lib/wiki_lyrics/cli/wikipluginadapter.rb', line 84

def wiki_process_response( response, response_plugin, wiki_searched )

  return false if ! submit?() || ! authorized?

  lyrics_found_on_site = response.lyrics && response_plugin == self
  lyrics_autogenerated = lyrics_found_on_site && ! response.custom_data["reviewed"]

  return false if lyrics_found_on_site && ! lyrics_autogenerated # we have nothing to do
  return false if (! @wpa_prompt_autogen && lyrics_autogenerated) || (! @wpa_prompt_no_lyrics && ! response.lyrics)

  begin

    check_submit_conditions()

    song_data = MediaWikiLyrics::SongData.new(
      response.artist ? response.artist : response.request.artist,
      response.title ? response.title : response.request.title,
      response.lyrics,
      response.album ? response.album : response.request.album,
      response.year ? response.year : response.request.year,
      response.custom_data["credits"],
      response.custom_data["lyricist"]
    )

    ,  = submit_song_page(
      song_data,
      lyrics_autogenerated ? response.url : nil, # are we editing an existing page or creating a new one?
      song_data.artist == response.request.artist && song_data.title == response.request.title &&
      wiki_searched && ! lyrics_found_on_site, # do we KNOW FOR SURE the page doesn't exists?
      @wpa_review || lyrics_autogenerated, # should we show the review contents dialog?
      lyrics_autogenerated # should user be forced to review contents? (true causes the operation to abort otherwise)
    )

    # transfer summited data to response
    if 
      response.artist = ["artist"] if ! Strings.empty?( ["artist"] )
      response.title = ["title"] if ! Strings.empty?( ["title"] )
      response.album = ["album"] if ! Strings.empty?( ["album"] )
      response.year = ["year"] if ! Strings.empty?( ["year"] )
      response.lyrics = ["lyrics"] if ! Strings.empty?( ["lyrics"] )
      custom_data = {}
      custom_data.merge( response.custom_data )
      custom_data["credits"] = ["credits"] if ! Strings.empty?( ["credits"] )
      custom_data["lyricst"] = ["lyricst"] if ! Strings.empty?( ["lyricst"] )
      response.custom_data = custom_data
    end

    return (@wpa_review || lyrics_autogenerated) && 

  rescue TimeoutError
    notify( I18n.get( "cli.application.plugintimeout", plugin_name(), site_host() ) )
    return false
  end

end

#write_config(config_file) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/wiki_lyrics/amarok/wikipluginadapter.rb', line 116

def write_config( config_file )

  if @wpa_submit
    username, password, cookie, last_auth_check, authorized = get_session_params()
  else
    cookie, last_auth_check, authorized = nil, nil, nil
  end

  values = {
    "#{plugin_name}_submit"       => @wpa_submit,
    "#{plugin_name}_review"       => @wpa_review,
    "#{plugin_name}_prompt_autogen"   => @wpa_prompt_autogen,
    "#{plugin_name}_prompt_no_lyrics" => @wpa_prompt_no_lyrics,
    "#{plugin_name}_username"     => Strings.scramble( @wpa_username ),
    "#{plugin_name}_password"     => Strings.scramble( @wpa_password ),
    "#{plugin_name}_cookie"       => cookie,
    "#{plugin_name}_last_auth_check"  => last_auth_check,
    "#{plugin_name}_authorized"     => authorized,
  }

  XMLHash.write( config_file, values )

end