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
;
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
;
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() {
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()) {
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()) {
74 public static FirefoxProfile
findProfileByNameOrDefault(@Nullable String name
, List
<FirefoxProfile
> profiles
) {
75 for (FirefoxProfile profile
: profiles
) {
76 if (profile
.getName().equals(name
)) {
80 return getDefaultProfile(profiles
);
84 public static FirefoxProfile
getDefaultProfile(List
<FirefoxProfile
> profiles
) {
85 if (profiles
.isEmpty()) return null;
87 for (FirefoxProfile profile
: profiles
) {
88 if (profile
.isDefault()) {
92 return profiles
.get(0);
96 public static List
<FirefoxProfile
> computeProfiles(File profilesFile
) {
97 if (!profilesFile
.isFile()) {
98 return Collections
.emptyList();
102 BufferedReader reader
= new BufferedReader(new FileReader(profilesFile
));
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;
112 @NonNls String line
= reader
.readLine();
121 if (line
.startsWith("[") && line
.endsWith("]")) {
122 if (!StringUtil
.isEmpty(currentPath
) && !StringUtil
.isEmpty(currentName
)) {
123 profiles
.add(new FirefoxProfile(currentName
, currentPath
, isDefault
, isRelative
));
129 insideProfile
= StringUtil
.startsWithIgnoreCase(line
, "[Profile");
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")) {
140 else if (name
.equalsIgnoreCase("name")) {
143 else if (name
.equalsIgnoreCase("default") && value
.equals("1")) {
146 else if (name
.equalsIgnoreCase("isrelative") && value
.equals("1")) {
157 catch (IOException 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
) {
170 new File(userHome
, "Library" + File
.separator
+ "Mozilla" + File
.separator
+ "Firefox"),
171 new File(userHome
, "Library" + File
.separator
+ "Application Support" + File
.separator
+ "Firefox"),
175 new File(userHome
, "Application Data" + File
.separator
+ "Mozilla" + File
.separator
+ "Firefox")