📄 downloadmanagerstateimpl.java
字号:
do_write = write_required;
write_required = false;
}finally{
this_mon.exit();
}
if ( do_write ){
try{
// System.out.println( "writing download state for '" + new String(torrent.getName()));
LGLogger.log( "Saving state for download '" + TorrentUtils.getLocalisedName( torrent ) + "'" );
TorrentUtils.writeToFile( torrent, true );
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}else{
// System.out.println( "not writing download state for '" + new String(torrent.getName()));
}
}
public void
delete()
{
try{
class_mon.enter();
state_map.remove( torrent.getHashWrapper());
TorrentUtils.delete( torrent );
}catch( TOTorrentException e ){
Debug.printStackTrace( e );
}finally{
class_mon.exit();
}
}
protected void
mergeTorrentDetails(
TOTorrent other_torrent )
{
try{
boolean write = TorrentUtils.mergeAnnounceURLs( other_torrent, torrent );
// System.out.println( "DownloadManagerState:mergeTorrentDetails -> " + write );
if ( write ){
save();
if ( download_manager != null ){
TRTrackerClient client = download_manager.getTrackerClient();
if ( client != null ){
// pick up any URL changes
client.resetTrackerUrl( false );
}
}
}
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
public void
setAttribute(
String name,
String value )
{
if ( name.equals( AT_CATEGORY )){
if ( value == null ){
setCategory( null );
}else{
Category cat = CategoryManager.getCategory( value );
if ( cat == null ){
cat = CategoryManager.createCategory( value );
}
setCategory( cat );
}
}else{
setStringAttribute( name, value );
}
}
public String
getAttribute(
String name )
{
if ( name.equals( AT_CATEGORY )){
Category cat = getCategory();
if ( cat == null ){
return( null );
}
if ( cat == CategoryManager.getCategory( Category.TYPE_UNCATEGORIZED )){
return( null );
}
return( cat.getName());
}else{
return( getStringAttribute( name ));
}
}
public
Category
getCategory()
{
return category;
}
public void
setCategory(
Category cat )
{
if ( cat == category ){
return;
}
if (cat != null && cat.getType() != Category.TYPE_USER){
cat = null;
}
Category oldCategory = (category == null)?CategoryManager.getCategory(Category.TYPE_UNCATEGORIZED):category;
category = cat;
if (oldCategory != null ){
oldCategory.removeManager( this );
}
if (category != null ){
category.addManager( this );
}
if ( category != null && category.getType() == Category.TYPE_USER ){
setStringAttribute( AT_CATEGORY, category.getName());
}else{
setStringAttribute( AT_CATEGORY, null );
}
}
protected String
getStringAttribute(
String attribute_name )
{
Map attributes = torrent.getAdditionalMapProperty( ATTRIBUTE_KEY );
if ( attributes == null ){
return( null );
}
byte[] bytes = (byte[])attributes.get( attribute_name );
if ( bytes == null ){
return( null );
}
try{
return( new String( bytes, Constants.DEFAULT_ENCODING ));
}catch( UnsupportedEncodingException e ){
Debug.printStackTrace(e);
return( null );
}
}
protected void
setStringAttribute(
final String attribute_name,
final String attribute_value )
{
Map attributes = torrent.getAdditionalMapProperty( ATTRIBUTE_KEY );
if ( attributes == null ){
if ( attribute_value == null ){
// nothing to do, no attributes and we're removing a value
return;
}
attributes = new HashMap();
torrent.setAdditionalMapProperty( ATTRIBUTE_KEY, attributes );
}
boolean changed = false;
if ( attribute_value == null ){
if ( attributes.containsKey( attribute_name )){
attributes.remove( attribute_name );
changed = true;
}
}else{
try{
byte[] existing_bytes = (byte[])attributes.get( attribute_name );
byte[] new_bytes = attribute_value.getBytes( Constants.DEFAULT_ENCODING );
if ( existing_bytes == null ||
!Arrays.equals( existing_bytes, new_bytes )){
attributes.put( attribute_name, new_bytes );
changed = true;
}
}catch( UnsupportedEncodingException e ){
Debug.printStackTrace(e);
}
}
if ( changed ){
write_required = true;
for (int i=0;i<listeners.size();i++){
try{
((DownloadManagerStateListener)listeners.get(i)).stateChanged(
this,
new DownloadManagerStateEvent()
{
public int
getType()
{
return( DownloadManagerStateEvent.ET_ATTRIBUTE_CHANGED );
}
public Object
getData()
{
return( attribute_name );
}
});
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
}
}
public static DownloadManagerState
getDownloadState(
DownloadManager dm )
{
return( new nullState(dm));
}
public void
addListener(
DownloadManagerStateListener l )
{
listeners.add( l );
}
public void
removeListener(
DownloadManagerStateListener l )
{
listeners.remove(l);
}
protected static class
nullState
implements DownloadManagerState
{
protected DownloadManager download_manager;
protected
nullState(
DownloadManager _dm )
{
download_manager = _dm;
}
public TOTorrent
getTorrent()
{
return( null );
}
public DownloadManager
getDownloadManager()
{
return( download_manager );
}
public void
clearResumeData()
{
}
public Map
getResumeData()
{
return( new HashMap());
}
public void
setResumeData(
Map data )
{
}
public void
clearTrackerResponseCache()
{
}
public Map
getTrackerResponseCache()
{
return( new HashMap());
}
public void
setTrackerResponseCache(
Map value )
{
}
public void
setAttribute(
String name,
String value )
{
}
public String
getAttribute(
String name )
{
return( null );
}
public Category
getCategory()
{
return( null );
}
public void
setCategory(
Category cat )
{
}
public void
save()
{
}
public void
delete()
{
}
public void
addListener(
DownloadManagerStateListener l )
{}
public void
removeListener(
DownloadManagerStateListener l )
{}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -