summaryrefslogtreecommitdiffstats
path: root/src/fbgui/html/js/networkDiscovery.js
blob: 10b4f1919f63d58bb6f886061848e747d3572a3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
var hideProgressIndicator = function () {
  $("#ajaxBusy").hide();
};



var updateTips = function ( t ) {
  $( ".validateTips" )
    .text( t )
    .addClass( "ui-state-highlight" );
  setTimeout(
    function() {
      $( ".validateTips" ).removeClass( "ui-state-highlight", 1500 );
    },
    500 );
};



var checkLength = function ( o, n, min, max ) {
  if ( o.val().length > max || o.val().length < min ) {
    o.addClass( "ui-state-error" );
    updateTips( "Length of " + n + " must be between " +
		min + " and " + max + "." );
    return false;
  } else {
    return true;
  }
};



var checkRegexp = function ( o, regexp, n ) {
  if ( !( regexp.test( o.val() ) ) ) {
    o.addClass( "ui-state-error" );
    updateTips( n );
    return false;
  } else {
    return true;
  }
};



var showLog = function (t) {
  $("#nd_show_log_msg").html(t);  
  //$("#nd_show_log_msg").attr('readonly','readonly');
  $('#nd_show_log_dialog').resizable("enable");
  $("#nd_show_log_dialog").dialog(
    { buttons: { "Cancel": function() {
                  $(this).dialog("close");}},
      minWidth: 920, 
      maxHeight: 620,
      modal: true,
      draggable: false,
      closeOnEscape: true,
      open: function(event, ui) {
        $(this).css({'max-height': 620, 'overflow-y': 'auto'}); 
    },
    });
};



var getInterfaceConfOnChange = function (i) {
  var n = i.options[i.selectedIndex].text;
  var jsonObj = fbgui.getInterfaceConf(n);
  if (!jQuery.isEmptyObject(jsonObj)) {
    $("#ipaddr").val(jsonObj.ipaddr);
    $("#netmask").val(jsonObj.netmask);
    $("#broadcast").val(jsonObj.broadcast);
    $("#gateway").val(jsonObj.gateway);
    $("#dns").val(jsonObj.dns);
  }

};



var getInterfaceConf = function (i) {
  var jsonObj = fbgui.getInterfaceConf(i);
  if (!jQuery.isEmptyObject(jsonObj)) {
    $("#ipaddr").val(jsonObj.ipaddr);
    $("#netmask").val(jsonObj.netmask);
    $("#broadcast").val(jsonObj.broadcast);
    $("#gateway").val(jsonObj.gateway);
    $("#dns").val(jsonObj.dns);
  }

};



var ip4_manualConfigurationDialog = function () {
  var jsonArr = fbgui.getManualConfInterfaces();
  if(jsonArr == "") {
    var c = "<p>Have not found usable interfaces for manual configuration</p>";
    $("#nd_manual_configuration_dialog").html(c);
    $("#nd_manual_configuration_dialog").dialog(
      { buttons: { "Cancel": function() { 
                     $(this).dialog("close");}},
        minWidth: 450, 
        modal: true,
        resizable: false,
        draggable: false,
        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide();}
      });

  } else {
      var c = "<select name='nd_mc_ifname_select' id='nd_mc_ifname_select' onChange='getInterfaceConfOnChange(this)'>";
      jQuery.each(jsonArr, function() {
                   c += "  <option>"+ this +"</option>";
                   }
      );
  
      c += "</select>";
      $("#nd_mc_ifname").html(c);
  
      var ifname = $("#nd_mc_ifname_select :selected").text(),
        ipaddr = $("#ipaddr"),
        netmask = $("#netmask"),
        broadcast = $("#broadcast"),
        gateway = $("#gateway"),
        dns = $("#dns"),
        allFields = $([]).add(ipaddr).add(netmask).add(broadcast).add(gateway).add(dns);

    getInterfaceConf(ifname);

    $("#nd_manual_configuration_dialog").dialog(
      { buttons: { "Cancel": function() { 
                     $(this).dialog("close");}, 
                   "Continue": function() {
                     var bValid = true;
                     allFields.removeClass("ui-state-error");

                     bValid = bValid && checkLength(ipaddr, "IP-Address", 7, 15);
                     bValid = bValid && checkLength(netmask, "Netmask Address", 7, 15);
                     bValid = bValid && checkLength(broadcast, "Broadcast Address", 7, 15);
                     bValid = bValid && checkLength(gateway, "Gateway Address", 7, 15);
                     bValid = bValid && checkLength(dns, "DNS Address", 7, 15);

                     bValid = bValid && checkRegexp(ipaddr, /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/, "Is not a valid IP-Address! Format has to be e.g.: 192.168.1.2 (max value is 255)");
                     bValid = bValid && checkRegexp(netmask, /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/, "Is not a valid IP-Address! Format has to be e.g.: 255.255.255.0 (max value is 255)");
                     bValid = bValid && checkRegexp(broadcast, /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/, "Is not a valid IP-Address! Format has to be e.g.: 192.168.1.255 (max value is 255)");
                     bValid = bValid && checkRegexp(gateway, /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/, "Is not a valid IP-Address! Format has to be e.g.: 192.168.1.254 (max value is 255)");
                     bValid = bValid && checkRegexp(dns, /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/, "Is not a valid IP-Address! Format has to be e.g.: 192.168.1.111 (max value is 255)");

                     if (bValid) {
                       // put variables into a json object
                       // send to qt networkdiscovery
                       var o = {"ifname" : ifname,
                                "ipaddr" : ipaddr.val(),
                                "netmask" : netmask.val(),
                                "broadcast" : broadcast.val(),
                                "gateway" : gateway.val(),
                                "dns" :  dns.val() }
                       try {
                         fbgui.ip4_setManualConfiguration(o);
                       } catch (e) {
                         fbgui.notifyCall(e);
                       }
                       $(this).dialog("close");
                     } }
                 } ,
        minWidth: 450, 
        modal: true,
        resizable: false,
        draggable: false,
        closeOnEscape: false, 
        close: function() {allFields.val("").removeClass("ui-state-error");},
        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide();}
      });
  }
};



var abortBootDialog = function (m) {
  fbgui.notifyCall("abortBootDialog");
  $("#nd_abort_boot_msg").html(m);
  $("#nd_abort_boot_dialog").dialog(
    { buttons: { "Manual Configure": function() {
                   ip4_manualConfigurationDialog();}, 
                 "Show Log": function() {
                   var text = fbgui.readLogFile(); 
                   showLog(text);}, 
                 "Restart": function() {fbgui.restartSystem();
                   $(this).dialog("close"); },
                 "Shut Down": function() { fbgui.shutDownSystem(); 
                   $(this).dialog("close"); },
		 "Try Again": function() {fbgui.tryAgain();
                   $(this).dialog("close"); }
               } ,
      minWidth: 450, 
      modal: true,
      resizable: false,
      draggable: false,
      closeOnEscape: false, 
      open: function(event, ui) { $(".ui-dialog-titlebar-close").hide();}
    });
};



var chooseInterfaceDialog = function (i) {
  var cb = "<label> Choose your interface: </label>"+
           "<select id='nd_ifName_select'>";
  jQuery.each(i, function() {
		   updateIfProgressBar(this, 100);
                   cb += "  <option>"+ this +"</option>";
                 }
  );
  cb += "</select>";

  $("#nd_choose_interface_msg").html(cb);
  $("#nd_choose_interface_dialog").dialog(
    { buttons: { "Manual Configure": function() {
                   ip4_manualConfigurationDialog();},
                 "Show Log": function() {
                   var text = fbgui.readLogFile(); 
                   showLog(text);}, 
                 "Restart": function() {fbgui.restartSystem();
                   $(this).dialog("close"); },
                 "Shut Down": function() { fbgui.shutDownSystem(); 
                   $(this).dialog("close"); },
                 "Continue": function() { 
                               var ifName = $("#nd_ifName_select :selected").text();
                               fbgui.continueBoot(ifName); 
                               $(this).dialog("close"); 
                             }
               } ,
      minWidth: 550, 
      modal: true,
      resizable: false,
      draggable: false,
      closeOnEscape: false, 
      open: function(event, ui) { $(".ui-dialog-titlebar-close").hide();}
    });
}



var updateStatus = function (s){
  $("#nd_status").html(s);
};



var updateIfProgressBar = function (i, p){
  if (p >= 0 && p <= 100){
    $("#"+i+"_progress").progressbar({ value: p });
  }else{
  $("#"+i+"_progress").progressbar({ value: 0 });
  }
};



var updateIfStatus = function(i, s){
  $("#"+i+"_status").html(s);
};



var addInterface = function (i){
  hideProgressIndicator();
  $("#nd_progress_container").append(
    "<div class='interface'>"+
    "  Interface: " +i+
    "  <span id='"+i+"_status'>Start DHCP </span>" +
    "  <div id='"+i+"_progress' class='progressbar' "+
    "       style='height: 10px;'></div>"+
    "</div>"
  );
  //$("#"+i+"_progress").progressbar({ value: 33 });
};