update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / ide / browsers / firefox / FirefoxUtil.java
blobd5a36c47c7cd84a37d3fe40008faa2c59f14bbf7
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ide.browsers.firefox;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.util.SystemInfo;
20 import com.intellij.openapi.util.text.StringUtil;
21 import com.intellij.util.SystemProperties;
22 import org.jetbrains.annotations.NonNls;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
34 /**
35 * @author nik
37 public class FirefoxUtil {
38 private static final Logger LOG = Logger.getInstance("#com.intellij.ide.browsers.firefox.FirefoxUtil");
39 @NonNls public static final String PROFILES_INI_FILE = "profiles.ini";
41 private FirefoxUtil() {
44 @Nullable
45 public static File getDefaultProfileIniPath() {
46 File[] roots = getProfilesDirs();
47 for (File profilesDir : roots) {
48 File profilesFile = new File(profilesDir, PROFILES_INI_FILE);
49 if (profilesFile.isFile()) {
50 return profilesFile;
53 return null;
56 @Nullable
57 public static File getFirefoxExtensionsDir(FirefoxSettings settings) {
58 File profilesFile = settings.getProfilesIniFile();
59 if (profilesFile != null && profilesFile.exists()) {
60 List<FirefoxProfile> profiles = computeProfiles(profilesFile);
61 FirefoxProfile profile = findProfileByNameOrDefault(settings.getProfile(), profiles);
62 if (profile != null) {
63 File profileDir = profile.getProfileDirectory(profilesFile);
64 File dir = new File(profileDir, "extensions");
65 if (dir.isDirectory()) {
66 return dir;
70 return null;
73 @Nullable
74 public static FirefoxProfile findProfileByNameOrDefault(@Nullable String name, List<FirefoxProfile> profiles) {
75 for (FirefoxProfile profile : profiles) {
76 if (profile.getName().equals(name)) {
77 return profile;
80 return getDefaultProfile(profiles);
83 @Nullable
84 public static FirefoxProfile getDefaultProfile(List<FirefoxProfile> profiles) {
85 if (profiles.isEmpty()) return null;
87 for (FirefoxProfile profile : profiles) {
88 if (profile.isDefault()) {
89 return profile;
92 return profiles.get(0);
95 @NotNull
96 public static List<FirefoxProfile> computeProfiles(File profilesFile) {
97 if (!profilesFile.isFile()) {
98 return Collections.emptyList();
101 try {
102 BufferedReader reader = new BufferedReader(new FileReader(profilesFile));
103 try {
104 final List<FirefoxProfile> profiles = new ArrayList<FirefoxProfile>();
105 boolean insideProfile = false;
106 String currentName = null;
107 String currentPath = null;
108 boolean isDefault = false;
109 boolean isRelative = false;
110 boolean eof = false;
111 while (!eof) {
112 @NonNls String line = reader.readLine();
113 if (line == null) {
114 eof = true;
115 line = "[]";
117 else {
118 line = line.trim();
121 if (line.startsWith("[") && line.endsWith("]")) {
122 if (!StringUtil.isEmpty(currentPath) && !StringUtil.isEmpty(currentName)) {
123 profiles.add(new FirefoxProfile(currentName, currentPath, isDefault, isRelative));
125 currentName = null;
126 currentPath = null;
127 isDefault = false;
128 isRelative = false;
129 insideProfile = StringUtil.startsWithIgnoreCase(line, "[Profile");
130 continue;
133 final int i = line.indexOf('=');
134 if (i != -1 && insideProfile) {
135 @NonNls String name = line.substring(0, i).trim();
136 @NonNls String value = line.substring(i + 1).trim();
137 if (name.equalsIgnoreCase("path")) {
138 currentPath = value;
140 else if (name.equalsIgnoreCase("name")) {
141 currentName = value;
143 else if (name.equalsIgnoreCase("default") && value.equals("1")) {
144 isDefault = true;
146 else if (name.equalsIgnoreCase("isrelative") && value.equals("1")) {
147 isRelative = true;
151 return profiles;
153 finally {
154 reader.close();
157 catch (IOException e) {
158 LOG.info(e);
160 return Collections.emptyList();
163 private static File[] getProfilesDirs() {
164 final String userHome = SystemProperties.getUserHome();
165 if (SystemInfo.isUnix) {
166 return new File[] {new File(userHome, ".mozilla" + File.separator + "firefox")};
168 if (SystemInfo.isMac) {
169 return new File[] {
170 new File(userHome, "Library" + File.separator + "Mozilla" + File.separator + "Firefox"),
171 new File(userHome, "Library" + File.separator + "Application Support" + File.separator + "Firefox"),
174 return new File[] {
175 new File(userHome, "Application Data" + File.separator + "Mozilla" + File.separator + "Firefox")