//# 0 errors, 39 messages
//#
/*
    //#DMDircResourceManager.java:1:1: class: com.dmdirc.util.resourcemanager.DMDircResourceManager
    //#DMDircResourceManager.java:1:1: method: com.dmdirc.util.resourcemanager.DMDircResourceManager.com.dmdirc.util.resourcemanager.DMDircResourceManager__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.util.resourcemanager;

import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.logger.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

/**
 *
 * @author chris
 */
public class DMDircResourceManager {
    //#DMDircResourceManager.java:36: method: void com.dmdirc.util.resourcemanager.DMDircResourceManager.com.dmdirc.util.resourcemanager.DMDircResourceManager()
    //#DMDircResourceManager.java:36: end of method: void com.dmdirc.util.resourcemanager.DMDircResourceManager.com.dmdirc.util.resourcemanager.DMDircResourceManager()

    /**
     * Returns the working directory for the application.
     * 
     * @return Current working directory
     */
    public static synchronized String getCurrentWorkingDirectory() {
        String path = "";        
    //#DMDircResourceManager.java:44: method: String com.dmdirc.util.resourcemanager.DMDircResourceManager.getCurrentWorkingDirectory()
    //#input(String getCurrentWorkingDirectory()): ""._tainted
    //#input(String getCurrentWorkingDirectory()): __Class_Obj.__Lock
    //#input(String getCurrentWorkingDirectory()): com.dmdirc.logger.ErrorLevel.MEDIUM
    //#output(String getCurrentWorkingDirectory()): java.lang.String:substring(...)._tainted
    //#output(String getCurrentWorkingDirectory()): return_value
    //#new obj(String getCurrentWorkingDirectory()): java.lang.String:substring(...)
    //#presumption(String getCurrentWorkingDirectory()): init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
    //#presumption(String getCurrentWorkingDirectory()): java.lang.ClassLoader:getResource(...)@45 != null
    //#presumption(String getCurrentWorkingDirectory()): java.lang.ClassLoader:getResource(...)@51 != null
    //#presumption(String getCurrentWorkingDirectory()): java.lang.String:lastIndexOf(...)@62 <= 4_294_967_294
    //#presumption(String getCurrentWorkingDirectory()): java.lang.System:getProperty(...)@56 != null
    //#presumption(String getCurrentWorkingDirectory()): java.lang.Thread:currentThread(...)@45 != null
    //#presumption(String getCurrentWorkingDirectory()): java.lang.Thread:currentThread(...)@51 != null
    //#presumption(String getCurrentWorkingDirectory()): java.lang.Thread:getContextClassLoader(...)@45 != null
    //#presumption(String getCurrentWorkingDirectory()): java.lang.Thread:getContextClassLoader(...)@51 != null
    //#presumption(String getCurrentWorkingDirectory()): java.net.URL:getPath(...)@54 != null
    //#post(String getCurrentWorkingDirectory()): java.lang.String:substring(...)._tainted == 0
    //#post(String getCurrentWorkingDirectory()): init'ed(return_value)
    //#test_vector(String getCurrentWorkingDirectory()): java.lang.String:equals(...)@50: {0}, {1}
    //#test_vector(String getCurrentWorkingDirectory()): java.lang.String:equals(...)@53: {0}, {1}
    //#test_vector(String getCurrentWorkingDirectory()): java.lang.String:startsWith(...)@56: {0}, {1}
        final URL resource = Thread.currentThread().getContextClassLoader().
                        getResource("com/dmdirc/Main.class");
        
        final String protocol = resource.getProtocol();
        
        if ("file".equals(protocol)) {
            path = Thread.currentThread().
                    getContextClassLoader().getResource("").getPath();
        } else if ("jar".equals(protocol)) {
            final String tempPath = resource.getPath();
            
            if (System.getProperty("os.name").startsWith("Windows")) {
                path = tempPath.substring(6, tempPath.length() - 23);
            } else {
                path = tempPath.substring(5, tempPath.length() - 23);
            }
            
            path = path.substring(0, path.lastIndexOf('/') + 1);
        }
        
        try {
            path = URLDecoder.decode(path, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.userError(ErrorLevel.MEDIUM, "Unable to decode path");
    //#DMDircResourceManager.java:68: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.logger.Logger:userError(ErrorLevel, String)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.util.resourcemanager.DMDircResourceManager
    //#    method: String getCurrentWorkingDirectory()
    //#    unanalyzed callee: void com.dmdirc.logger.Logger:userError(ErrorLevel, String)
            path = "";
        }
        return path;
    //#DMDircResourceManager.java:71: end of method: String com.dmdirc.util.resourcemanager.DMDircResourceManager.getCurrentWorkingDirectory()
    }
    
    /**
     * Determines if this instance of DMDirc is running from a jar or not.
     * 
     * @return True if this instance is running from a JAR, false otherwise
     */
    public static boolean isRunningFromJar() {
        final URL resource = Thread.currentThread().getContextClassLoader().
    //#DMDircResourceManager.java:80: method: bool com.dmdirc.util.resourcemanager.DMDircResourceManager.isRunningFromJar()
    //#output(bool isRunningFromJar()): return_value
    //#presumption(bool isRunningFromJar()): java.lang.ClassLoader:getResource(...)@80 != null
    //#presumption(bool isRunningFromJar()): java.lang.Thread:currentThread(...)@80 != null
    //#presumption(bool isRunningFromJar()): java.lang.Thread:getContextClassLoader(...)@80 != null
    //#post(bool isRunningFromJar()): init'ed(return_value)
                        getResource("com/dmdirc/Main.class");
        return "jar".equals(resource.getProtocol());
    //#DMDircResourceManager.java:82: end of method: bool com.dmdirc.util.resourcemanager.DMDircResourceManager.isRunningFromJar()
    }
    
}
    //#output(com.dmdirc.util.resourcemanager.DMDircResourceManager__static_init): __Descendant_Table[com/dmdirc/util/resourcemanager/DMDircResourceManager]
    //#post(com.dmdirc.util.resourcemanager.DMDircResourceManager__static_init): __Descendant_Table[com/dmdirc/util/resourcemanager/DMDircResourceManager] == &__Dispatch_Table
    //#DMDircResourceManager.java:: end of method: com.dmdirc.util.resourcemanager.DMDircResourceManager.com.dmdirc.util.resourcemanager.DMDircResourceManager__static_init
    //#DMDircResourceManager.java:: end of class: com.dmdirc.util.resourcemanager.DMDircResourceManager
