summaryrefslogtreecommitdiffstats
path: root/scilab/modules/atoms/macros/atoms_gui/cbAtomsGui.sci
blob: 1872e399c7a57d560830b56d5e7cf3a8162c6613 (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2009 - DIGITEO - Vincent COUVERT <vincent.couvert@scilab.org>
// Copyright (C) 2009-2010 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution. The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt

// Internal function

function cbAtomsGui()

    // Load Atoms Internals lib if it's not already loaded
    if ~ exists("atomsinternalslib") then
        load("SCI/modules/atoms/macros/atoms_internals/lib");
    end

    UItag = get(gcbo,"Tag");

    // Get the description frame object
    DescFrame = findobj("tag","DescFrame");

    // Display selected module informations
    // =========================================================================

    if or(UItag == ["LeftListbox";"HomeListbox"]) then

        // Get the selected module
        module = getSelected(UItag);

        // Save the module name
        set(DescFrame,"userdata",module);

        // Update the description frame
        updateDescFrame();

        // Show the description frame
        showDesc();
    end

    // A button has been pressed
    // =========================================================================

    if or(UItag == ["installButton";"removeButton";"updateButton"]) then
        // Get the module name
        module = get(DescFrame,"userdata");

        // Disable callbacks
        disableAtomsGui();
    end

    // Install selected module
    // =========================================================================

    if UItag == "installButton" then
        if execstr("atomsInstall("""+module+""")","errcatch")<>0 then
            messagebox(gettext("Installation failed!"),gettext("Atoms error"),"error");
        else
            messagebox(gettext("Installation done! Please restart Scilab to take changes into account."),gettext("Atoms"),"info");
        end

    // Remove selected module
    // =========================================================================

    elseif UItag == "removeButton" then // Remove selected module
        if execstr("atomsRemove("""+module+""")", "errcatch")<>0 then
            messagebox(gettext("Remove failed!"),gettext("Atoms error"),"error");
        else
            messagebox(gettext("Remove done! Please restart Scilab to take changes into account. "),gettext("Atoms"),"info");
        end

    // Update selected module
    // =========================================================================

    elseif UItag == "updateButton" then // Update selected module
        if execstr("atomsUpdate("""+module+""")","errcatch")<>0 then
            messagebox(gettext("Update failed!"),gettext("Atoms error"),"error");
        else
            messagebox(gettext("Update done! Please restart Scilab to take changes into account."),gettext("Atoms"),"info");
        end
    end

    // End of the button action
    // =========================================================================

    if or(UItag == ["installButton";"removeButton";"updateButton"]) then
        updateDescFrame();
        enableLeftListbox();
    end


    // Menu
    // =========================================================================

    // File:Home
    if UItag == "homeMenu" then
        showHome();

    // File:Close
    elseif UItag == "closeMenu" then
        delete(findobj("Tag", "atomsFigure"));

    // ?:Help
    elseif UItag == "helpMenu" then
        help("atoms")

    end

endfunction


// =============================================================================
// getSelected()
//  + Return the module name selected from a listbox.
//  + Accepted listboxes:
//    - LeftListbox
//    - HomeListbox
// =============================================================================

function name = getSelected(listbox)

    selected = get(findobj("Tag",listbox),"Value");

    if listbox == "HomeListbox" then
        modulesNames       = atomsGetInstalled();
        modulesNames       = modulesNames(:,1);
    elseif listbox == "LeftListbox" then
        allModules         = get(findobj("Tag", "atomsFigure"), "userdata");
        modulesNames       = getfield(1, allModules);
        modulesNames (1:2) = [];
    else
        error(msprintf(gettext("%s: Wrong value for input argument #%d: ''%s'' or ''%s'' expected.\n"),"getSelected",1,"HomeListbox","LeftListbox"));
    end

    name = modulesNames(selected);

endfunction


// =============================================================================
// disableAtomsGui()
//  + Disable all callback
// =============================================================================


function disableAtomsGui()

    set(findobj("tag", "installButton"), "Enable", "off");
    set(findobj("tag", "updateButton") , "Enable", "off");
    set(findobj("tag", "removeButton") , "Enable", "off");
    disableLeftListbox()
endfunction

function disableLeftListbox()
    set(findobj("tag", "LeftListbox"),"Callback", "");
    set(findobj("tag", "LeftListbox"),"ForegroundColor",[0.5 0.5 0.5]);
endfunction

function enableLeftListbox()
    set(findobj("tag", "LeftListbox"),"Callback", "cbAtomsGui");
    set(findobj("tag", "LeftListbox"),"ForegroundColor",[0 0 0]);
endfunction


// =============================================================================
// updateDescFrame()
//  + Update the description frame with the selected module
//  + does not change the description frame visibility
// =============================================================================

function updateDescFrame()

    // Operating system detection + Architecture detection
    // =========================================================================
    [OSNAME,ARCH,LINUX,MACOSX,SOLARIS,BSD] = atomsGetPlatform();

    // Get the modules list and the selected module
    // =========================================================================

    thisFigure     = findobj("tag","atomsFigure");

    Desc           = findobj("tag","Desc");
    DescFrame      = findobj("tag","DescFrame");
    DescTitle      = findobj("tag","DescTitle");

    allModules     = get(thisFigure,"userdata");
    thisModuleName = get(DescFrame ,"userdata");

    // Reset the message frame
    // =========================================================================
    set(findobj("tag","msgText"),"String","");

    // Get the module details
    // =========================================================================

    modulesNames       = getfield(1, allModules);
    modulesNames (1:2) = [];
    thisModuleStruct   = allModules(thisModuleName);

    MRVersionAvailable = atomsGetMRVersion(thisModuleName);
    MRVersionInstalled = "";

    if atomsIsInstalled(thisModuleName) then
        MRVersionInstalled = atomsVersionSort(atomsGetInstalledVers(thisModuleName),"DESC");
        MRVersionInstalled = MRVersionInstalled(1);
        thisModuleDetails  = thisModuleStruct(MRVersionInstalled);
    else
        thisModuleDetails  = thisModuleStruct(MRVersionAvailable);
    end

    // Manage size
    // =========================================================================

    if isfield(thisModuleDetails,OSNAME+ARCH+"Size") then
        sizeHTML = ..
            "<div style=""font-weight:bold;margin-top:10px;margin-bottom:5px;"">" + ..
            gettext("Download size")                                              + ..
            "</div>"                                                              + ..
            "<div>"                                                               + ..
            atomsSize2human(thisModuleDetails(OSNAME+ARCH+"Size"))                + ..
            "</div>";
    else
        sizeHTML = "";
    end

    // Manage authors
    // =========================================================================

    authorMat  = thisModuleDetails.Author;

    authorHTML = "<div style=""font-weight:bold;margin-top:10px;margin-bottom:5px;"">" + ..
                 gettext("Author(s)") + ..
                 "</div>" + ..
                 "<div>";

    for i=1:size(authorMat,"*")
        authorHTML = authorHTML + authorMat(i)+"<br>";
    end

    authorHTML = authorHTML + "</div>";

    // URLs (See also)
    // =========================================================================

    URLs        = [];
    seeAlsoHTML = "";

    if isfield(thisModuleDetails,"URL") & (thisModuleDetails.URL<>"") then
        URLs = [ URLs ; thisModuleDetails.URL ];
    end

    if isfield(thisModuleDetails,"WebSite") & (thisModuleDetails.WebSite<>"") then
        URLs = [ URLs ; thisModuleDetails.WebSite ];
    end

    if ~isempty(URLs) then

        seeAlsoHTML = "<div style=""font-weight:bold;margin-top:10px;margin-bottom:5px;"">" + ..
                      gettext("See also") + ..
                      "</div>" + ..
                      "<div>";

        for i=1:size(URLs,"*")
            seeAlsoHTML = seeAlsoHTML + "&nbsp;&bull;&nbsp;"+URLs(i)+"<br>";
        end

        seeAlsoHTML = seeAlsoHTML + "</div>";

    end

    // Release date
    // =========================================================================

    if isfield(thisModuleDetails,"Date") ..
       & ~isempty(regexp(thisModuleDetails.Date,"/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]\s/")) then

        dateHTML = "<div style=""font-weight:bold;margin-top:10px;margin-bottom:5px;"">" ..
                   + gettext("Release date") ..
                   + "</div>" ..
                   + "<div>" ..
                   + part(thisModuleDetails.Date,1:10) ..
                   + "</div>";
    else
        dateHTML = "";
    end

    // Build and Set the HTML code
    // =========================================================================


    htmlcode = "<html>" + ..
               "<body>" + ..
               "<div style=""font-weight:bold;margin-top:10px;margin-bottom:5px;"">" + ..
               gettext("Version") + ..
               "</div>" + ..
               "<div>" + thisModuleDetails.Version  + "</div>" + ..
               authorHTML + ..
               "<div style=""font-weight:bold;margin-top:10px;margin-bottom:5px;"">" + ..
               gettext("Description") + ..
               "</div>" + ..
               "<div>" + ..
               strcat(thisModuleDetails.Description,"<br>")  + ..
               "</div>" + ..
               seeAlsoHTML + ..
               dateHTML + ..
               sizeHTML + ..
               "</body>" + ..
               "</html>";

    // Process URLs and Emails
    htmlcode = processHTMLLinks(htmlcode);

    // Update the main description
    set(Desc,"String",htmlcode);

    // Description title management
    // =========================================================================

    descFrameHTML    = thisModuleDetails.Title;

    descFramePos     = get(DescTitle,"Position");
    descFramePos(3)  = 300;

    set(DescTitle, "String"   , descFrameHTML );
    set(DescTitle, "Position" , descFramePos );

    // Buttons
    // =========================================================================

    // Tests for update available
    // --------------------------

    canUpdate = "off";

    if atomsIsInstalled(thisModuleName) & atomsVersionCompare(MRVersionInstalled,MRVersionAvailable) == -1 then
        // Not up-to-date
        canUpdate = "on";
        showWarning(sprintf(gettext("A new version (''%s'') of ''%s'' is available"),MRVersionAvailable,thisModuleDetails.Title));
    end

    // Can be removed
    // --------------------------

    if atomsIsInstalled(thisModuleName) then
        canRemove = "on";
    else
        canRemove = "off";
    end

    // Can be installed
    // --------------------------

    if ~ atomsIsInstalled(thisModuleName) then
        canInstall = "on";
    else
        canInstall = "off";
    end

    // Update the buttons
    // --------------------------

    set(findobj("tag", "installButton"), "Enable", canInstall);
    set(findobj("tag", "updateButton") , "Enable", canUpdate );
    set(findobj("tag", "removeButton") , "Enable", canRemove );

endfunction

// =============================================================================
// atomsSize2human()
// =============================================================================

function human_str = atomsSize2human(size_str)

    size_int = strtod(size_str);

    if size_int < 1024 then
        human_str = string(size_int) + " " + gettext("Bytes");

    elseif size_int < 1024*1024 then
        human_str = string(round(size_int/1024)) + " " + gettext("KB");

    else
        human_str = string( round((size_int*10)/(1024*1024)) / 10 ) + " " + gettext("MB");

    end

endfunction

// =============================================================================
// show()
// =============================================================================

function show(tag)
    obj = findobj("tag",tag);
    set(obj,"Visible","On");
endfunction

// =============================================================================
// hide()
// =============================================================================

function hide(tag)
    obj = findobj("tag",tag);
    set(obj,"Visible","Off");
endfunction

// =============================================================================
// showHome()
// + Hide the detailed description of a module
// + Show the home page
// =============================================================================

function showHome()

    // Reset the message frame
    set(findobj("tag","msgText"),"String","");

    // Hide the Desc frame
    hide("DescFrame");
    hide("DescTitle");
    hide("Desc");
    hide("removeButton");
    hide("installButton");
    hide("updateButton");

    // Show the Home page
    show("HomeFrame");
    show("HomeTitle");
    show("HomeListbox");

endfunction

// =============================================================================
// showDesc()
// + Hide the home page
// + Show the detailed description of a module
// =============================================================================

function showDesc()

    // Show the Home page
    hide("HomeFrame");
    hide("HomeTitle");
    hide("HomeListbox");

    // Hide the Desc frame
    show("DescFrame");
    show("DescTitle");
    show("Desc");
    show("removeButton");
    show("installButton");
    show("updateButton");

endfunction


// =============================================================================
// showWarning
// + Update the string in the msg Frame
// =============================================================================

function showWarning(msg)

    str =       "<html>";
    str = str + "<table><tr>";
    str = str + "<td><img src=""file:///"+SCI+"/modules/atoms/images/icons/software-update-available.png"" /></td>";
    str = str + "<td><div style=""color:red;font-style:italic;"">"+msg+"</div></td>";
    str = str + "</tr></table>";
    str = str + "</html>";

    msgText = findobj("tag","msgText");

    set(msgText,"String",str);

endfunction

// =============================================================================
// processHTMLLinks
// + Find URLs and emails
// + Convert them in HTML
// =============================================================================

function txtout = processHTMLLinks(txtin)

    regexUrl   = "/((((H|h)(T|t)|(F|f))(T|t)(P|p)((S|s)?))\:\/\/)(www|[a-zA-Z0-9])[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\:[0-9]{1,5})*(\/($|[a-zA-Z0-9\.\,\;\?\''\\\+&amp;%\$#\=~_\-\/]+))*/";
    regexEmail = "/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/";
    txtout     = "";

    // Process URLs

    [mat_start,mat_end,mat_match] = regexp(txtin,regexUrl);

    if ~isempty(mat_match) then
        mat_end = [ 0 mat_end ];
        for i=1:size(mat_match,"*")
            txtout = txtout + part(txtin,[mat_end(i)+1:mat_start(i)-1]) ..
                            + "<a href="""+mat_match(i)+""" target=""_blank"">" ..
                            + mat_match(i) ..
                            + "</a>";
        end
        txtout = txtout + part(txtin,mat_end(size(mat_end,"*"))+1:length(txtin));
        txtin  = txtout;
        txtout = "";
    end

    // Process Emails

    [mat_start,mat_end,mat_match] = regexp(txtin,regexEmail);

    if ~isempty(mat_match) then
        mat_end = [ 0 mat_end ];
        for i=1:size(mat_match,"*")
            txtout = txtout + part(txtin,[mat_end(i)+1:mat_start(i)-1]) ..
                            + "<a href=""mailto:"+mat_match(i)+""" target=""_blank"">" ..
                            + mat_match(i) ..
                            + "</a>";
        end
        txtout = txtout + part(txtin,mat_end(size(mat_end,"*"))+1:length(txtin));
    else
        txtout = txtin;
    end

endfunction