~ubuntu-branches/ubuntu/quantal/freeguide/quantal

« back to all changes in this revision

Viewing changes to src/program/lib/fgspecific/ConfigGuesser.java

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2006-09-30 10:14:18 UTC
  • mfrom: (1.2.3 upstream) (3.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060930101418-pkilk36yy22nbt3r
Tags: 0.10.4-2
Update the watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  FreeGuide J2
3
 
 *
4
 
 *  Copyright (c) 2001-2004 by Andy Balaam and the FreeGuide contributors
5
 
 *
6
 
 *  freeguide-tv.sourceforge.net
7
 
 *
8
 
 *  Released under the GNU General Public License
9
 
 *  with ABSOLUTELY NO WARRANTY.
10
 
 *
11
 
 *  See the file COPYING for more information.
12
 
 */
13
 
 
14
 
package freeguide.lib.fgspecific;
15
 
 
16
 
import freeguide.*;
17
 
import java.io.BufferedReader;
18
 
import java.io.File;
19
 
import java.io.FileReader;
20
 
 
21
 
/**
22
 
 *  A static class to check configuration options.
23
 
 *
24
 
 *@author     Andy Balaam
25
 
 *@created    28 June 2003
26
 
 *@version    4
27
 
 */
28
 
 
29
 
public class ConfigGuesser {
30
 
 
31
 
    /**
32
 
     *  Description of the Method
33
 
     *
34
 
     *@param  group  Description of the Parameter
35
 
     *@param  entry  Description of the Parameter
36
 
     *@param  val    Description of the Parameter
37
 
     *@return        Description of the Return Value
38
 
     */
39
 
    public static String checkValue(String group, String entry, Object val) {
40
 
 
41
 
                if( group.equals( "misc" ) ) {
42
 
                        
43
 
                        if( entry.equals("day_start_time") ) {
44
 
                                return checkValidTime( (String)val, "the FreeGuide day start" );
45
 
                        } else if( entry.equals( "grabber_start_time" ) ) {
46
 
                                return checkValidTime( (String)val, "the grabber start time" );
47
 
                        } else if( entry.equals( "days_to_grab" ) ) {
48
 
                                return checkNumeric( (String)val, "the number of days to grab");
49
 
                        } else if( entry.equals( "working_directory" ) ) {
50
 
                                return checkDirWriteable( (File)val, "a working directory");
51
 
                        } else if( entry.equals( "install_directory" ) ) {
52
 
                                return checkDirWriteable( (File)val, "the install directory");
53
 
                        } else if( entry.equals( "region" ) ) {
54
 
                                return checkNonEmpty( (String)val, "region" );
55
 
                        } else if( entry.equals( "browser" ) ) {
56
 
                                return null;
57
 
                        } else if( entry.equals( "privacy" ) ) {
58
 
                                return null;
59
 
                        }
60
 
                        
61
 
                }
62
 
                
63
 
                String msg = "Check asked for on an unknown option \"" + group + "."
64
 
                        + entry + "\".";
65
 
                
66
 
                if( FreeGuide.log != null ) {
67
 
                        FreeGuide.log.info( msg );
68
 
                } else {
69
 
                        System.err.println( msg );
70
 
                }
71
 
                
72
 
                // If it's not one of these, don't check , just say it's good.
73
 
                return null;
74
 
                
75
 
    }
76
 
 
77
 
 
78
 
    /**
79
 
     *  Description of the Method
80
 
     *
81
 
     *@param  time    Description of the Parameter
82
 
     *@param  option  Description of the Parameter
83
 
     *@return         Description of the Return Value
84
 
     */
85
 
    private static String checkValidTime(String time, String whatFor) {
86
 
                
87
 
        if (time != null && time.length() == 5 && time.charAt(2) == ':') {
88
 
            return null;
89
 
        }
90
 
        return "The time you gave for " + whatFor
91
 
                        + " is not in valid hh:mm format.";
92
 
    }
93
 
 
94
 
 
95
 
        /**
96
 
     *  Description of the Method
97
 
     *
98
 
     *@param  number  Description of the Parameter
99
 
     *@param  option  Description of the Parameter
100
 
     *@return         Description of the Return Value
101
 
     */
102
 
    private static String checkNumeric(String number, String whatFor) {
103
 
                
104
 
                if( number == null ) {
105
 
                        return "The option for " + whatFor + " is blank.";
106
 
                }
107
 
                
108
 
                try {
109
 
                        Integer.parseInt( number );
110
 
                } catch( NumberFormatException e ) {
111
 
                        
112
 
                        return "The option for " + whatFor + " is not a valid number.";
113
 
                        
114
 
                }
115
 
                
116
 
        return null;
117
 
    }
118
 
        
119
 
        /**
120
 
     *  Description of the Method
121
 
     *
122
 
     *@param  number  Description of the Parameter
123
 
     *@param  option  Description of the Parameter
124
 
     *@return         Description of the Return Value
125
 
     */
126
 
    private static String checkNonEmpty(String number, String whatFor) {
127
 
                
128
 
                if( number == null || number.equals("") ) {
129
 
                        return "The option for " + whatFor + " is blank.";
130
 
                }
131
 
                
132
 
        return null;
133
 
    }
134
 
        
135
 
    /**
136
 
     *  Description of the Method
137
 
     *
138
 
     *@param  dir      Description of the Parameter
139
 
     *@param  whatFor  Description of the Parameter
140
 
     *@return          Description of the Return Value
141
 
     */
142
 
    private static String checkDirWriteable(File dir, String whatFor) {
143
 
        PreferencesGroup prefs = new PreferencesGroup();
144
 
 
145
 
        dir = new File(prefs.performSubstitutions(dir.toString()));
146
 
 
147
 
        try {
148
 
 
149
 
            if (!dir.exists()) {
150
 
                dir.mkdirs();
151
 
            }
152
 
 
153
 
            // Check it exists and you can write a file to it
154
 
            File testFile = new File(dir.getPath() + "test.tmp");
155
 
            testFile.createNewFile();
156
 
            if (dir.exists() && testFile.canWrite()) {
157
 
 
158
 
                testFile.delete();
159
 
                return null;
160
 
            }
161
 
            testFile.delete();
162
 
            return "The directory you chose for " + whatFor
163
 
                                + " can't be created or isn't writeable.";
164
 
                        
165
 
        } catch (java.io.IOException e) {
166
 
 
167
 
            return "There was an error trying to create the directory you "
168
 
                                + "chose for " + whatFor + ".";
169
 
        }
170
 
    }
171
 
 
172
 
    private final static String fs = File.separator;
173
 
 
174
 
}