View Javadoc

1   /*
2    * $Id: Win32.java 125 2008-11-04 07:18:28Z arnep $
3    */
4   package net.sf.ovanttasks.ovnative.win32;
5   
6   import java.io.File;
7   
8   /**
9    * Win32 contains common used static methods to retrieve windows specific
10   * directories like desktop, start menu, sent to menu etc.
11   * Most directories can be queried for the current user or all users.
12   * 
13   * @author lars.gersmann@roxes.com
14   * @author arnep@users.sf.net
15   */
16  public class Win32 {
17  
18      static {
19          OvNativeLibLoader.loadLib();
20      }
21  
22      /**
23       * SpecialDirectory holds wll known directories.
24       */
25      public static enum SpecialDirectory {
26  
27          PROGRAM_FILES, COMMON_FILES, COMMON_DESKTOP, COMMON_DOCUMENTS, COMMON_START_MENU, COMMON_PROGRAMS_MENU, COMMON_AUTOSTART_MENU, COMMON_TEMPLATES, PERSONAL_DESKTOP, PERSONAL_FILES, PERSONAL_START_MENU, PERSONAL_PROGRAMS_MENU, PERSONAL_AUTOSTART_MENU, PERSONAL_TEMPLATES, PERSONAL_SEND_TO, PERSONAL_FAVORITES, TEMP, TMP;
28          private File dir;
29  
30          public String getPath() {
31              return getDir().getPath();
32          }
33  
34          public File getDir() {
35              if (dir == null) {
36                  Registry reg;
37                  switch (this) {
38                      case COMMON_FILES:
39                          reg = new Registry(Registry.HKEY_LOCAL_MACHINE,
40                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
41                          dir = new File(reg.getStringValue("CommonFilesDir"));
42                          break;
43                      case PROGRAM_FILES:
44                          reg = new Registry(Registry.HKEY_LOCAL_MACHINE,
45                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
46                          dir = new File(reg.getStringValue("ProgramFilesDir"));
47                          break;
48                      case COMMON_DESKTOP:
49                          reg = new Registry(Registry.HKEY_LOCAL_MACHINE,
50                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
51                          dir = new File(reg.getStringValue("Common Desktop"));
52                          break;
53                      case COMMON_DOCUMENTS:
54                          reg = new Registry(Registry.HKEY_LOCAL_MACHINE,
55                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
56                          dir = new File(reg.getStringValue("Common Documents"));
57                          break;
58                      case COMMON_START_MENU:
59                          reg = new Registry(Registry.HKEY_LOCAL_MACHINE,
60                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
61                          dir = new File(reg.getStringValue("Common Start Menu"));
62                          break;
63                      case COMMON_PROGRAMS_MENU:
64                          reg = new Registry(Registry.HKEY_LOCAL_MACHINE,
65                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
66                          dir = new File(reg.getStringValue("Common Programs"));
67                          break;
68                      case COMMON_AUTOSTART_MENU:
69                          reg = new Registry(Registry.HKEY_LOCAL_MACHINE,
70                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
71                          dir = new File(reg.getStringValue("Common Startup"));
72                          break;
73                      case COMMON_TEMPLATES:
74                          reg = new Registry(Registry.HKEY_LOCAL_MACHINE,
75                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
76                          dir = new File(reg.getStringValue("Common Templates"));
77                          break;
78                      case PERSONAL_AUTOSTART_MENU:
79                          reg = new Registry(Registry.HKEY_CURRENT_USER,
80                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
81                          dir = new File(reg.getStringValue("Startup"));
82                          break;
83                      case PERSONAL_DESKTOP:
84                          reg = new Registry(Registry.HKEY_CURRENT_USER,
85                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
86                          dir = new File(reg.getStringValue("Desktop"));
87                          break;
88                      case PERSONAL_FILES:
89                          reg = new Registry(Registry.HKEY_CURRENT_USER,
90                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
91                          dir = new File(reg.getStringValue("Personal"));
92                          break;
93                      case PERSONAL_FAVORITES:
94                          reg = new Registry(Registry.HKEY_CURRENT_USER,
95                                  "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
96                          dir = new File(reg.getStringValue("Favorites"));
97                          break;
98                      case PERSONAL_PROGRAMS_MENU:
99                          reg = new Registry(Registry.HKEY_CURRENT_USER,
100                                 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
101                         dir = new File(reg.getStringValue("Programs"));
102                         break;
103                     case PERSONAL_SEND_TO:
104                         reg = new Registry(Registry.HKEY_CURRENT_USER,
105                                 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
106                         dir = new File(reg.getStringValue("SendTo"));
107                         break;
108                     case PERSONAL_START_MENU:
109                         reg = new Registry(Registry.HKEY_CURRENT_USER,
110                                 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
111                         dir = new File(reg.getStringValue("Start Menu"));
112                         break;
113                     case PERSONAL_TEMPLATES:
114                         reg = new Registry(Registry.HKEY_CURRENT_USER,
115                                 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
116                         dir = new File(reg.getStringValue("Templates"));
117                         break;
118                     case TEMP:
119                         dir = new File(expandEnvironmentStrings("%TEMP%"));
120                         break;
121                     case TMP:
122                         dir = new File(expandEnvironmentStrings("%TMP%"));
123                         break;
124 
125                     default:
126                         throw new Win32Exception(
127                                 "getSpecialFolder failed : folder id " + name() + " is unknown");
128                 }
129             }
130             return dir;
131         }
132     }
133 
134     /**
135      * Expands a value %VALUE% 
136      * @param value
137      * @return
138      * @throws net.sf.ovanttasks.ovnative.win32.Win32Exception
139      */
140     public native static String expandEnvironmentStrings(String value)
141             throws Win32Exception;
142 
143     public native static String getWindowsDirectory() throws Win32Exception;
144 
145     public native static String getWindowsSystemDirectory()
146             throws Win32Exception;
147 
148     @Deprecated
149     public static File getSpecialDirectory(SpecialDirectory folder) {
150         return folder.getDir();
151     }
152 }