📄 platformmanagerimpl.java
字号:
/*
* Created on 18-Apr-2004
* Created by Paul Gardner
* Copyright (C) 2004, 2005, 2006 Aelitis, All Rights Reserved.
*
* 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.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* AELITIS, SAS au capital de 46,603.30 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*
*/
package org.gudy.azureus2.platform.win32;
/**
* @author parg
*
*/
import org.gudy.azureus2.core3.logging.*;
import org.gudy.azureus2.core3.util.AEMonitor;
import org.gudy.azureus2.core3.util.Debug;
import org.gudy.azureus2.core3.util.SystemProperties;
import org.gudy.azureus2.platform.PlatformManager;
import org.gudy.azureus2.platform.PlatformManagerCapabilities;
import org.gudy.azureus2.platform.PlatformManagerListener;
import org.gudy.azureus2.platform.win32.access.AEWin32Access;
import org.gudy.azureus2.platform.win32.access.AEWin32AccessListener;
import org.gudy.azureus2.platform.win32.access.AEWin32Manager;
import org.gudy.azureus2.plugins.platform.PlatformManagerException;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class
PlatformManagerImpl
implements PlatformManager, AEWin32AccessListener
{
private static final LogIDs LOGID = LogIDs.CORE;
public static final int RT_NONE = 0;
public static final int RT_AZ = 1;
public static final int RT_OTHER = 2;
public static final String DLL_NAME = "aereg";
public static final String NEW_MAIN_ASSOC = "Azureus";
public static final String OLD_MAIN_ASS0C = "BitTorrent";
private static boolean init_tried;
private static PlatformManagerImpl singleton;
private static AEMonitor class_mon = new AEMonitor( "PlatformManager");
private final Set capabilitySet = new HashSet();
private List listeners = new ArrayList();
public static PlatformManagerImpl
getSingleton()
throws PlatformManagerException
{
try{
class_mon.enter();
if ( singleton == null && !init_tried ){
init_tried = true;
try{
singleton = new PlatformManagerImpl( AEWin32Manager.getAccessor());
}catch( Throwable e ){
Logger.log(new LogEvent(LOGID, "Win32Platform: failed to initialise", e ));
if ( e instanceof PlatformManagerException ){
throw((PlatformManagerException)e);
}
throw( new PlatformManagerException( "Win32Platform: failed to initialise", e ));
}
}
return( singleton );
}finally{
class_mon.exit();
}
}
protected AEWin32Access access;
protected String app_exe_name;
protected File az_exe;
protected boolean az_exe_checked;
protected
PlatformManagerImpl(
AEWin32Access _access )
throws PlatformManagerException
{
access = _access;
access.addListener( this );
app_exe_name = SystemProperties.getApplicationName() + ".exe";
initializeCapabilities();
applyPatches();
}
private void
initializeCapabilities()
{
capabilitySet.add(PlatformManagerCapabilities.CreateCommandLineProcess);
capabilitySet.add(PlatformManagerCapabilities.GetUserDataDirectory);
capabilitySet.add(PlatformManagerCapabilities.RecoverableFileDelete);
capabilitySet.add(PlatformManagerCapabilities.RegisterFileAssociations);
capabilitySet.add(PlatformManagerCapabilities.ShowFileInBrowser);
capabilitySet.add(PlatformManagerCapabilities.GetVersion);
capabilitySet.add(PlatformManagerCapabilities.SetTCPTOSEnabled);
}
protected void
applyPatches()
{
try{
File exe_loc = getApplicationEXELocation();
String az_exe_string = exe_loc.toString();
//int icon_index = getIconIndex();
String current =
access.readStringValue(
AEWin32Access.HKEY_CLASSES_ROOT,
NEW_MAIN_ASSOC + "\\DefaultIcon",
"" );
//System.out.println( "current = " + current );
String target = az_exe_string + "," + getIconIndex();
//System.out.println( "target = " + target );
// only patch if Azureus.exe in there
if ( current.indexOf( app_exe_name ) != -1 && !current.equals(target)){
access.writeStringValue(
AEWin32Access.HKEY_CLASSES_ROOT,
NEW_MAIN_ASSOC + "\\DefaultIcon",
"",
target );
}
}catch( Throwable e ){
//e.printStackTrace();
}
}
protected int
getIconIndex()
throws PlatformManagerException
{
/*
File exe_loc = getAureusEXELocation();
long size = exe_loc.length();
boolean old_exe = size < 250000;
return( old_exe?0:1);
*/
// weird, seems like it should be 0 for old and new
return( 0 );
}
public String
getVersion()
{
return( access.getVersion());
}
protected File
getApplicationEXELocation()
throws PlatformManagerException
{
if ( az_exe == null ){
try{
String az_home;
try{
az_home = access.getApplicationInstallDir( SystemProperties.getApplicationName());
az_exe = new File( az_home + File.separator + app_exe_name ).getAbsoluteFile();
if ( !az_exe.exists()){
throw( new PlatformManagerException( app_exe_name + " not found in " + az_home + ", please re-install"));
}
}catch( Throwable e ){
//hmmm, well let's try the app dir
az_home = SystemProperties.getApplicationPath();
az_exe = new File( az_home + File.separator + app_exe_name ).getAbsoluteFile();
}
if ( !az_exe.exists()){
String msg = app_exe_name + " not found in " + az_home + " - can't check file associations. Please re-install " + SystemProperties.getApplicationName();
az_exe = null;
if (!az_exe_checked){
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING,
msg));
}
throw( new PlatformManagerException( msg ));
}
}finally{
az_exe_checked = true;
}
}
return( az_exe );
}
public int
getPlatformType()
{
return( PT_WINDOWS );
}
public String
getUserDataDirectory()
throws PlatformManagerException
{
try{
return( access.getUserAppData());
}catch( Throwable e ){
throw( new PlatformManagerException( "Failed to read registry details", e ));
}
}
public File
getLocation(
long location_id )
throws PlatformManagerException
{
if ( location_id == LOC_USER_DATA ){
return(new File(getUserDataDirectory()));
}else if ( location_id == LOC_MUSIC ){
try{
return( new File(
access.readStringValue(
AEWin32Access.HKEY_CURRENT_USER,
"software\\microsoft\\windows\\currentversion\\explorer\\shell folders",
"My Music" )));
}catch( Throwable e ){
throw( new PlatformManagerException( "Failed to read registry details", e ));
}
}else{
return( null );
}
}
public String
getApplicationCommandLine()
{
try{
return( getApplicationEXELocation().toString());
}catch( Throwable e ){
return( null );
}
}
public boolean
isApplicationRegistered()
throws PlatformManagerException
{
try{
// always trigger magnet reg here if not owned so old users get it...
if ( getAdditionalFileTypeRegistrationDetails( "Magnet", ".magnet" ) == RT_NONE ){
registerMagnet();
}
}catch( Throwable e ){
Debug.printStackTrace(e);
}
if ( isAdditionalFileTypeRegistered( OLD_MAIN_ASS0C, ".torrent" )){
registerAdditionalFileType( NEW_MAIN_ASSOC, "BitTorrent File", ".torrent", "application/x-bittorrent" );
}
return( isAdditionalFileTypeRegistered( NEW_MAIN_ASSOC, ".torrent" ));
}
public boolean
isAdditionalFileTypeRegistered(
String name,
String type )
throws PlatformManagerException
{
return( getAdditionalFileTypeRegistrationDetails( name, type ) == RT_AZ );
}
public int
getAdditionalFileTypeRegistrationDetails(
String name,
String type )
throws PlatformManagerException
{
String az_exe_str;
try{
az_exe_str = getApplicationEXELocation().toString();
}catch( Throwable e ){
return( RT_NONE );
}
try{
String test1 =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -