
    fD                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dl	mZmZmZmZmZ d dlmZ d dlmZmZ d dlZd dlZ ej*                  d e
j,                                dZdZd	Zd
ZdZ ej8                   e
j:                               j=                  d      Z ej8                   e
j@                               j=                  d      Z!e jD                  jG                  e!jI                         d      Z%e jD                  jG                  e!jI                         d      Z&e jD                  jG                  e!jI                         d      Z' G d de
jP                        Z) ejT                  e)       y)    N)RB)GObjectGtkGdkGioGLib)TrackListHandler)DownloadAlbumHandlerMagnatuneDownloadError	rhythmboxubuntuz+http://magnatune.com/info/song_info_xml.zipz%http://magnatune.com/info/changed.txtz#http://magnatune.com/member/signup?z?http://%s:%s@download.magnatune.com/buy/membership_free_dl_xml?	magnatunesong_info.xmlzsong_info.zip.tmpzchanged.txtc                   l    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)MagnatuneSourcec                    t         j                  j                  |        | | _        d | _        t
        j                  j                  d      | _        d| _	        d | _
        d | _        i | _        i | _        i | _        d| _        d| _        d | _        d | _        d | _        d | _        i | _        i | _        t        j.                  d      | _        y )Nz%org.gnome.rhythmbox.plugins.magnatuneFr   z	album-art)name)r   BrowserSource__init__hate_MagnatuneSource__popupr   Settingsnew_MagnatuneSource__settings_MagnatuneSource__activated_MagnatuneSource__db_MagnatuneSource__info_screen_MagnatuneSource__sku_dict_MagnatuneSource__home_dict_MagnatuneSource__art_dict_MagnatuneSource__has_loaded_MagnatuneSource__update_id"_MagnatuneSource__catalogue_loader!_MagnatuneSource__catalogue_check_MagnatuneSource__load_progress#_MagnatuneSource__download_progress_MagnatuneSource__downloads_MagnatuneSource__copiesExtDB_MagnatuneSource__art_storeselfs    H/usr/lib/x86_64-linux-gnu/rhythmbox/plugins/magnatune/MagnatuneSource.pyr   zMagnatuneSource.__init__?   s    D!$)$,LL$$%LM$/$$)$ $/$$/ $$ $$$!$ $$-XX;/$    c           	         | j                   ct        j                         }|j                  t	        j
                  | j                  j                  d             |j                  d      | _         t        j                  j                  | j                         }|j                  | d        |j                  d d d d dt        j                                y )Nzmagnatune-popup.uizmagnatune-popup   )r   r   Builderadd_from_filerbfind_plugin_filepropsplugin
get_objectMenunew_from_modelattach_to_widgetpopupget_current_event_time)r,   buildermenus      r-   do_show_entry_popupz#MagnatuneSource.do_show_entry_popupa   s    	\\[[]7
,,TZZ->->@TUV$$%674<		 	 	.$d#**T4tQ(B(B(DEr.   c                 N   | j                   s| j                  j                  }|j                  j                  | _        | j                  j
                  | _        t        j                  d       s*t        j                         }t        j                  |d       t        j                  d       s*t        j                         }t        j                  |d       d| _         | j                  d       t        j                  d| j                         | _        | j!                          y y )Ni  Ti`T  )r   r5   shelldbr   
entry_type_MagnatuneSource__entry_typemagnatune_in_progress_dirquery_existsget_pathosmkdirmagnatune_cache_dir%_MagnatuneSource__show_loading_screenr   timeout_add_seconds"_MagnatuneSource__update_cataloguer"   )r,   rA   magnatune_in_progress_pathmagnatune_cache_paths       r-   do_selectedzMagnatuneSource.do_selectedl   s    			::5{{~~49zz,,4
#
0
0
6!:!C!C!EHH'/

*
*4
0.779HH!5)4d# ..{D<S<ST4% 
r.   c                      y)NF r+   s    r-   do_can_deletezMagnatuneSource.do_can_delete   s    	r.   c                     t        j                  dd      | _        | j                  | j                  ddd       | j                  j                  |ddd       y )NF   )homogeneousspacingTr   )r   VBox_MagnatuneSource__paned_box
pack_start)r,   contents     r-   do_pack_contentzMagnatuneSource.do_pack_content   sG    XX%;$//$""D$2gtT15r.   c                 `   | j                   dk7  r&t        j                  | j                          d| _         | j                  !| j                  j	                          d | _        | j
                  !| j
                  j	                          d | _        t        j                  j                  |        y Nr   )	r"   r   source_remover#   cancelr$   r   r   do_delete_thyselfr+   s    r-   ra   z!MagnatuneSource.do_delete_thyself   s    	d&&'4	(!!#!4	'  " 4$$T*r.   c                    | j                   j                  j                   j                  j                         }| j	                         j                         }t        |      dk(  ry |d   }| j                  |j                  t        j                  j                           }| j                  |   }t        j                  ||t        j                          y r^   )r5   rA   window
get_screenget_entry_viewget_selected_entrieslenr   
get_stringr   RhythmDBPropTypeLOCATIONr   r   show_urir   CURRENT_TIME)r,   screentrackstrskuurls         r-   display_artist_infoz#MagnatuneSource.display_artist_info   s    ::!!((335& 557&[A	ay"b&9&9&B&BCD##,,vsC,,-r.   c                 p   | j                   j                  j                   j                  j                         }| j	                         j                         }t        |      dk(  ry t        t        j                  j                  dt        i      z   }t        j                  ||t        j                         y )Nr   ref)r5   rA   rc   rd   re   rf   rg   magnatune_member_signup_uriurllibparse	urlencodemagnatune_partner_idr   rk   r   rl   )r,   rm   rn   rq   s       r-   download_redirectz!MagnatuneSource.download_redirect   s    ::!!((335& 557&[A	#fll&<&<uFZ=\&]]#,,vsC,,-r.   c                     | j                   d   dk7  r| j                          y 	 t        j                  j	                  d      }|d   d   }| j                         j                         }g }|D ]_  }| j                  |j                  t        j                  j                           }||v r>|j                  |       | j!                  |       a y # t
        $ r3}t        j                  t        d      t        d             Y d }~y d }~ww xY w)	Nzaccount-typedownloadorg.gnome.rhythmbox.rhythmdb	locationsr   zCouldn't download albumz:You must have a library location set to download an album.titlemessage)r   rz   r   r   r   
IndexErrorr   error_dialog_re   rf   r   rh   ri   rj   append_MagnatuneSource__auth_download)r,   librarylibrary_locationern   skustrackrp   s           r-   download_albumzMagnatuneSource.download_album   s    	__^$
2	
\\<=7k*1-  557&	$ e	))"*=*=*F*FG	H3	Tk;;s 
 
??167TUW	
s   'C 	D)DDc                       fd} fd fdt        j                          _         j                  j                  t        |       y )Nc                    d _         	 t        t        d      }|j                         j	                         }| j	                         j                  d      } t        d|d|        || k7  r=	 t        t        d      }|j                  | dz          |j                                   y j                  d	u r         y y #  d}Y xY w# t        $ r!}t        dt        |      z         Y d }~Md }~ww xY w)
Nrt z
iso-8859-1zlocal checksum z, remote checksum wt
z#unable to write local change id: %sF)r$   openmagnatune_changesreadstripdecodeprintwriteclose	Exceptionstrr!   )remote_changesflocal_changesr   download_catalogueload_cataloguer,   s       r-   	update_cbz5MagnatuneSource.__update_catalogue.<locals>.update_cb   s     4%AFFHNN$M #((*11,?>M>	RS~%;		&QWW^d"#WWY U" 	#M  ;
03q69::;s#   .C .4C
 C
	C4C//C4c                  N   d d } fd}	 t        j                  t              }|j                  d        t        j
                  j                         _        t        d      j                  j                  _
        j                  j                  j                  j                  j                  j                         t	        j                         _        j                  j!                  |        j                  j#                  t$        t        |       y #  Y xY w)Nc                     | j                         D ]+  }|j                  j                  d      s|j                  c S  y )Nr   )infolistfilenameendswith)	catalogueinfos     r-   find_song_infozVMagnatuneSource.__update_catalogue.<locals>.download_catalogue.<locals>.find_song_info   s;    ""$ /]] r.   c                 f    t        t        |      |z  d      |j                  j                  _        y )N      ?)minfloatr%   r5   task_progress)copycompletetotalr,   s       r-   download_progresszYMagnatuneSource.__update_catalogue.<locals>.download_catalogue.<locals>.download_progress   s&    /25?U3JC/PD,r.   c                 z   |s%t        d       t        | j                                y t        d       t        j                  t              }t        t        d      } |      }|*t        j                  t        d      t        d             y |j                  |j                  |             |j                          |j                          t        j                  t              }|j                  d        d |_        t        j"                  j$                  |j&                  j(                  _                 y )Nzcatalog download failedzcatalog download successfulwbzUnable to load catalogzHRhythmbox could not understand the Magnatune catalog, please file a bug.r   )r   	get_errorzipfileZipFilemagnatune_song_info_tempr   magnatune_song_infor   r   r   r   r   r   r   file_new_for_pathdeleter#   TaskOutcomeCOMPLETEr%   r5   task_outcome)	r   successr,   catalog_zipcatalogr   dfr   r   s	          r-   download_finishedzYMagnatuneSource.__update_catalogue.<locals>.download_catalogue.<locals>.download_finished   s    
$%
4>>	
'(//":;K&-Gk*H__156[\^MM+""8,-MMO			7	8BIIdO"D.0nn.E.ED+r.   Loading Magnatune catalog)r   r   r   r   r   TaskProgressSimpler   r%   r   r5   
task_labelrA   	task_listadd_task	AsyncCopy _MagnatuneSource__catalog_loaderset_progressstartmagnatune_song_info_uri)r   r   r   r   r   r,   s      @r-   r   z>MagnatuneSource.__update_catalogue.<locals>.download_catalogue   s    Q6				7	8BIIdO //3354+,-H+I4(::##,,T-A-AB<<>4%%&7>68PRceij	s   *D   D$c            	         fd} d_         t        j                  j                         _        t        d      j                  j                  _        j                  j                  j                  j                  j                  j                         ddit        j                  j                         }|j                  t        j                   j"                  j$                  j&                  j(                               t        j*                         _        j,                  j/                  | |       j,                  j1                  t2        d       y )Nc                    |/t         j                  j                  j                  j                  _        | j                         }|rt        d|z         	 |j                          j                  d       d _        t        j                  dt         j"                  j$                  d       D ]  }|j'                         }|j)                  d      s%t        j+                  |      j-                  d       \  }}	}
|	j/                  d      }	t        d|	z         j1                  |	|dd          y |j3                         j/                  dd	
      }|j5                  dd      }|j5                  dd      }|j5                  dd      }	 |j7                  |       dxx   t9        |      z  cc<   t;        t=        d         |z  d      j                  j                  _        y # t        j                  j                  $ r}t        d|z         Y d }~d }~ww xY w# t        j                  j                  $ r}t        d|z         Y d }~d }~ww xY w)Nzerror loading catalogue: %szerror parsing catalogue: %sFzstandard::namein_progress_utf-8zrestarting download from %s   replace)errors'-zRock & RollzRock &amp; Rollsizer   ) r   r   r   r%   r5   r   r   r   r   xmlsaxSAXParseExceptionrK   r#   rE   enumerate_childrenr   FileQueryInfoFlagsNONEget_name
startswithresolve_relative_pathload_contentsr    _MagnatuneSource__download_albumget_datar   feedrg   r   r   r   )loaderchunkr   parsererrorr   r   r   resulturietagdata	load_sizer,   s               r-   catalogue_chunk_cbzVMagnatuneSource.__update_catalogue.<locals>.load_catalogue.<locals>.catalogue_chunk_cb  s   }/1~~/F/FT,U)E12/lln
 
&#T (::;KSMcMcMhMhjno ,ZZ\d__^,5KKDQ__`devsDJJwc)C/0
Cbc+, NN##GI#>TLL%TLL%T LL(9:T/kk$ v#d)#03E)F:K4Lu4TVY0ZT-C GG%% /)A-../: GG%% /)A-../s0   G( H! (HHH!I>IITr   r   r   i   )r!   r   r   r   r%   r   r5   r   rA   r   r   r   r   make_parsersetContentHandlerr	   r   rD   r   r   r    ChunkLoaderr#   set_callbackr   r   )r   r   r   r,   s     @r-   r   z:MagnatuneSource.__update_catalogue.<locals>.load_catalogue  s    +[\ 4//3354+,-H+I4(::##,,T-A-AB{9GG!6	,TYY8I8I4??\`\l\lnrn}n}~^^-4''(:FC  !4g>r.   )r3   Loaderr$   get_urlmagnatune_changed_uri)r,   r   r   r   s   ` @@r-   __update_cataloguez"MagnatuneSource.__update_catalogue   s<    ,1kf<?~ 99;$  !6	Br.   c                    | j                   t        j                         }|j                  t	        j
                  | j                  j                  d             |j                  d      | _         | j                  | j                   ddd       | j                         j                  d       | j                   j                  d       | j                   j                  d|       | j                  j                  d|        y )Nzmagnatune-loading.ui magnatune_loading_scrolledwindowTr   visible)r   r   r1   r2   r3   r4   r5   r6   r7   rZ   re   set_no_show_allset_propertyrY   )r,   showr=   s      r-   __show_loading_screenz%MagnatuneSource.__show_loading_screen]  s    	[[]7
,,TZZ->->@VWX**+MN4??4%%tT15((.%%d+!!)T2	t84r.   c                      y NrR   r+   s    r-   __notify_status_changedz'MagnatuneSource.__notify_status_changedj  s    r.   c                 >     fd}t        dz          t        j                         }|j                         \  }}}t        d}t
        ||fz  }|t        j                  j                  |      z   }t        j                         }	|	j                  ||||f       y )Nc                 &   |\  }}t        j                  d         }t        j                  j	                         }|j                  |       | y 	 | j                  d      } | j                  dd      } | j                  dd      } |j                  |        |j                          t        j                  j                  |j                        }|d|d|j                  }t        j                   j#                  t        j$                  j'                  |j                               }|d	   }	t        j$                  j)                  t        j                   j+                  |d
   t        j                  j-                  |	                  }
|d
   ||
f|dd  z   }t        j                  j/                  |      }t1        dd|       j3                  |       y # t4        $ r?}t7        j8                  t;        d      t;        d      t=        |      z         Y d }~y d }~wt>        $ rc}tA        jB                  tA        jD                           t7        j8                  t;        d      t;        d      t=        |      z         Y d }~y d }~ww xY w)Nformatr   z<br>r   z & z &amp; :@   r   r0   zdownload uri for z is zDownload Errorz[An error occurred while trying to authorize the download.
The Magnatune server returned:
%sr   ErrorzKAn error occurred while trying to download the album.
The error text is:
%s)#r
   r   r   r   r   r   r   r   r   r   rv   rw   urlparserq   hostnamerH   pathsplitrequesturl2pathnamepathname2urljoinquote
urlunparser   r   r   r   r   r   r   r   sys
excepthookexc_info)r   userpassusernamepassworddl_album_handlerauth_parserparsednetlocspathbasenamer  authedaudio_dl_urir   r,   rp   s                 r-   auth_data_cbz5MagnatuneSource.__auth_download.<locals>.auth_data_cbs  s   "Hh*4??8+DE$$&;  !12
l
m;;wD<<#D<<y)DT \\""#3#7#78F#Xv?FGGMM&..55fkkBCEQxH>>&&rww||E!Hfll>P>PQY>Z'[\DQi&3F<<**62L	l
;<,,
  }OOA./qruxyzu{{} }
 mNNCLLN#OOAgJabehijekkm mms&   FG 	J'5H!!J-AJJzdownloading album: )idrp   )r   MagnatuneAccountinstancegetry   magnatune_api_download_urirv   rw   rx   r3   r   r   )
r,   rp   r#  accountaccount_typer  r  url_dictrq   ls
   ``        r-   __auth_downloadzMagnatuneSource.__auth_downloadq  s    &mP #$%%'''.{{}$<8	
( 	#h%99#fll$$X..#iik!))C(34r.   c                    	
 d 

fd}	
fd} fd	fdt         j                  d|z         t         j                  |      j                  j                  d      d dt        j
                  j                  t        j
                  j                  z  d         j                  t        j                  j                          _        t        d	       j                  j                  _         j                  j                  d
 j                           j                  j"                  j                  j$                  j'                   j                          j(                  vrgt        j*                         }|j-                  |        |j/                  j1                         |        d j2                  <   | j(                  <   y y )Nc                 z   t        | j                        dk(  r;t        j                  j                  | j
                  j                  _        d | _        y t        t        t        | j                  j                                \  }}|dkD  r2t        t        |      |z  d      | j
                  j                  _        y y )Nr   r   )rg   r'   r   r   r   r&   r5   r   mapsumzipvaluesr   r   r   )r,   r   r   s      r-   update_progressz9MagnatuneSource.__download_album.<locals>.update_progress  s    	$

q 24..2I2ID""/#D#sD$4$4$;$;$=>?OHeqy47h%8OQT4UT##1 r.   c                 8    ||f|j                   <    |       y r  )r'   )r   r   r   r,   r"  r4  s       r-   r   z;MagnatuneSource.__download_album.<locals>.download_progress  s     %-u$54L!4r.   c                     |j                   = |j                  = t        dd|       |r%t        j                        j                          n          |       y )Nzdownload of z finished: )target)r'   r(   r   	threadingThreadr   )r   r   r,   r"  remove_download_filesunzip_albumr4  s      r-   r   z;MagnatuneSource.__download_album.<locals>.download_finished  sR    %}}\",	@AK(..04r.   c                  >   t         j                  j                  d      } t        j                  | d   d         }t	        dj                         z         t        j                  j                               }|j                         D ]  }|j                  |      j                         }t	        d|d|       t        j                  |      }t        j                  |       t        j                  |      j                  d dt         j                  j                   d       }||j#                  |j%                  |      d        |j'                  d        t	        d|z         j(                  j+                  |        |j'                                   y )	Nr}   r~   r   zunzipping %szzip file entry: z => Fzadding %s to library)r   r   r   file_new_for_urir   rG   r   r   namelistr   get_urir   sanitize_uri_for_filesystemuri_create_parent_dirsr   FileCreateFlagsr   r   r   r   r   add_uri)	r   r   albumr   	track_uri	track_outdestr:  r,   s	         r-   r;  z5MagnatuneSource.__download_album.<locals>.unzip_album  s<   \\<=7**7;+?+BC$--/	)*??4==?+5  "u 66u=EEGI	y
9:..y9Ii($$Y/77eSEXEXE]E]_cdI__UZZ&-__T
!I-.	YYy!" 	;;=r.   c                  `    t        d       j                  d         j                  d        y )Nzremoving download files)r   r   )rG  in_progresss   r-   r:  z?MagnatuneSource.__download_album.<locals>.remove_download_files  s&    	"#d;;tr.   r   r   FzDownloading from Magnatunezcancel-task)r   r   )rE   r   replace_contentsencoder   rB  PRIVATEREPLACE_DESTINATIONr&   r   r   r   r   r5   r   connectcancel_downloadsrA   r   r   r(   r   r   r   r?  r'   )r,   r"  rp   r   r   dlrG  rI  r:  r;  r4  s   ``    @@@@@r-   __download_albumz MagnatuneSource.__download_album  sr   V
0
 *??QT@TU+	"	8	8	=$|227;





%
%c&9&9&M&M
M
	 
% 337794/01M/N4!!,##M43H3HI::##,,T-E-EF &
2??$d+88L$,,.*;TB$*4L!!#4== 'r.   c                     | j                   j                         D ]  }|j                           t        j                  j
                  |j                  _        y r  )r(   r3  r`   r   r   	CANCELLEDr5   r   )r,   taskr|   s      r-   rO  z MagnatuneSource.cancel_downloads  s@    --&&( h?? NN44$**r.   c                 l   | j                   r|sy |j                         | j                   j                  d      k7  ry | j                  |j	                  t
        j                  j                           }t
        j                  j                  d|j	                  t
        j                  j                              }|j                  d|j	                  t
        j                  j                               | j                  j                  |t
        j                  j                   | j"                  |          y )Nr   rD  artist)r   get_entry_typeentry_type_get_by_namer   rh   r   ri   rj   ExtDBKeycreate_storageALBUM	add_fieldARTISTr*   	store_uriExtDBSourceTypeEMBEDDEDr    )r,   entryrp   keys       r-   playing_entry_changedz%MagnatuneSource.playing_entry_changed  s    	%	
tyy??LL	(()<)<)E)EFG#
""7E,<,<R=P=P=V=V,WX#--%**2+>+>+E+EFGS""4"4"="=ts?STr.   N)__name__
__module____qualname__r   r?   rP   rS   r\   ra   rr   rz   r   rM   rK   '_MagnatuneSource__notify_status_changedr   r   rO  rc  rR   r.   r-   r   r   >   s[    0DF*6+&	..:JCZ555pM$^5	Ur.   r   )+rH   r  r   urllib.parserv   urllib.requestr8  r   r3   gi.repositoryr   r   r   r   r   r   r	   r
   r   r%  gettextinstall
locale_dirry   r   r   ru   r(  r   user_data_dirr   rE   user_cache_dirrJ   r  r  rG   r   r   r   r   r   type_registerrR   r.   r-   <module>rq     sG  8 
 
 
 #   	  6 6 - M   ]R]]_ -  H ? C ^ 1C112B"2B2B2DE[[\gh +c++,=B,=,=,?@VVWbc ggll#6#?#?#A?S 77<<(;(D(D(FH[\ GGLL!4!=!=!?O IUb&& IUV   o &r.   