From: Calin Crisan <ccrisan@gmail.com>
Date: Sun, 27 Oct 2013 09:29:45 +0000 (+0200)
Subject: added an update button
X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=344664adfc2bb238a03c6e0d3e6c30c1a941e880;p=motioneye-debian

added an update button
---

diff --git a/src/handlers.py b/src/handlers.py
index 81829aa..b18e8b1 100644
--- a/src/handlers.py
+++ b/src/handlers.py
@@ -33,9 +33,12 @@ class BaseHandler(RequestHandler):
         return data
     
     def render(self, template_name, content_type='text/html', **context):
+        import motioneye
+        
         self.set_header('Content-Type', content_type)
         
         context['USER'] = self.current_user
+        context['VERSION'] = motioneye.VERSION
         
         content = template.render(template_name, **context)
         self.finish(content)
@@ -512,13 +515,14 @@ class SnapshotHandler(BaseHandler):
     
     @BaseHandler.auth(prompt=False)
     def current(self, camera_id):
+        self.set_header('Content-Type', 'image/jpeg')
+        
         camera_config = config.get_camera(camera_id)
         if camera_config['@proto'] == 'v4l2':
             jpg = mjpgclient.get_jpg(camera_id)
             if jpg is None:
                 return self.finish()
         
-            self.set_header('Content-Type', 'image/jpeg')
             self.finish(jpg)
         
         else:
@@ -527,7 +531,6 @@ class SnapshotHandler(BaseHandler):
                     self.finish({})
                     
                 else:
-                    self.set_header('Content-Type', 'image/jpeg')
                     self.finish(jpg)
             
             remote.current_snapshot(
@@ -593,8 +596,15 @@ class UpdateHandler(BaseHandler):
         stable = self.get_argument('stable', default='false') == 'true'
         
         versions = update.get_all_versions(stable=stable)
-        
-        self.finish_json(versions)
+        current_version = update.get_version()
+        update_version = None
+        if versions and update.compare_versions(versions[-1], current_version) > 0:
+            update_version = versions[-1]
+
+        self.finish_json({
+            'update_version': update_version,
+            'current_version': current_version
+        })
 
     @BaseHandler.auth(admin=True)
     def post(self):
@@ -603,5 +613,5 @@ class UpdateHandler(BaseHandler):
         logging.debug('performing update to version %(version)s' % {'version': version})
         
         result = update.perform_update(version)
-
+        
         return self.finish_json(result)
diff --git a/src/update.py b/src/update.py
index 961a000..aafcbad 100644
--- a/src/update.py
+++ b/src/update.py
@@ -40,10 +40,7 @@ def get_all_versions(stable=False):
         if stable:
             versions = [v for v in versions if v.count('.') == 1]
 
-        versions.sort()
-        
-        return ['master'] # TODO
-        return versions
+        return sorted(versions, cmp=compare_versions)
 
     except Exception as e:
         logging.error('could not get versions: %(msg)s' % {'msg': unicode(e)})
diff --git a/static/css/main.css b/static/css/main.css
index 97e2451..c2ce587 100644
--- a/static/css/main.css
+++ b/static/css/main.css
@@ -253,6 +253,28 @@ img.apply-progress {
     margin-top: 3px;
 }
 
+div.update-button {
+    position: relative;
+    width: 4em;
+    height: 1.5em;
+    line-height: 1.5em;
+    text-align: center;
+    margin: 2px 0px;
+    color: white;
+    font-size: 1em;
+    background-color: #317CAD;
+    border-radius: 3px;
+    transition: all 0.1s linear;
+}
+
+div.update-button:HOVER {
+    background-color: #3498db;
+}
+
+div.update-button:ACTIVE {
+    background-color: #317CAD;
+}
+
 div.settings-top-bar.open select.video-device {
     display: inline;
 }
diff --git a/static/css/ui.css b/static/css/ui.css
index fefb05a..b9c6cc2 100644
--- a/static/css/ui.css
+++ b/static/css/ui.css
@@ -302,7 +302,7 @@ div.modal-container {
 }
 
 div.modal-title-bar {
-    height: 1.5em;
+    min-height: 1.5em;
     line-height: 1.5em;
     text-align: center;
     position: relative;
@@ -418,7 +418,7 @@ tr:HOVER span.help-mark {
     }
     
     div.modal-title-bar {
-        height: 2em;
+        min-height: 2em;
         line-height: 2em;
     }
     
diff --git a/static/js/main.js b/static/js/main.js
index b0abe55..037949b 100644
--- a/static/js/main.js
+++ b/static/js/main.js
@@ -758,7 +758,7 @@ function doApply() {
         }
         
         if (data.reload) {
-            window.location.reload();
+            window.location.reload(true);
             return;
         }
         
@@ -805,6 +805,32 @@ function doRemCamera() {
     });
 }
 
+function doUpdate() {
+    showModalDialog('<div class="modal-progress"></div>');
+    ajax('GET', '/update/?stable=true', null, function (data) {
+        if (data.update_version == null) {
+            runAlertDialog('motionEye is up to date (current version: ' + data.current_version + ')');
+        }
+        else {
+            runConfirmDialog('New version available: ' + data.update_version + '. Update?', function () {
+                showModalDialog('<div class="modal-progress"></div>');
+                ajax('POST', '/update/?version=' + data.update_version, null, function (result) {
+                    if (result) {
+                        runAlertDialog('motionEye was successfully updated!', function () {
+                            window.location.reload(true);
+                        });
+                    }
+                    else {
+                        runAlertDialog('Update failed!', function () {
+                            window.location.reload(true);
+                        });
+                    }
+                });
+            });
+        }
+    });
+}
+
 
     /* fetch & push */
 
@@ -937,8 +963,8 @@ function pushPreview() {
 
     /* dialogs */
 
-function runAlertDialog(message) {
-    runModalDialog({title: message, buttons: 'ok'});
+function runAlertDialog(message, onOk) {
+    runModalDialog({title: message, buttons: 'ok', onOk: onOk});
 }
 
 function runConfirmDialog(message, onYes) {
@@ -1461,6 +1487,9 @@ $(document).ready(function () {
         }
     });
     
+    /* software update button */
+    $('div#updateButton').click(doUpdate);
+    
     /* prevent scroll events on settings div from propagating TODO this does not work */
     $('div.settings').mousewheel(function (e, d) {
         var t = $(this);
diff --git a/static/js/ui.js b/static/js/ui.js
index a0f8a42..43a5d85 100644
--- a/static/js/ui.js
+++ b/static/js/ui.js
@@ -493,6 +493,12 @@ function showModalDialog(content, onClose) {
     var glass = $('div.modal-glass');
     var container = $('div.modal-container');
     
+    if (container.is(':animated')) {
+        return setTimeout(function () {
+            showModalDialog(content, onClose);
+        }, 100);
+    }
+    
     if (container.is(':visible')) {
         /* the modal dialog is already visible,
          * we just replace the content */
@@ -523,6 +529,12 @@ function hideModalDialog() {
     var glass = $('div.modal-glass');
     var container = $('div.modal-container');
     
+    if (container.is(':animated')) {
+        return setTimeout(function () {
+            hideModalDialog();
+        }, 100);
+    }
+    
     glass.animate({'opacity': '0'}, 200, function () {
         glass.css('display', 'none');
     });
@@ -576,11 +588,11 @@ function makeModalDialogButtons(buttonsInfo) {
         if (info.click) {
             var oldClick = info.click;
             info.click = function () {
+                hideModalDialog();
+                
                 if (oldClick() == false) {
                     return;
                 }
-                
-                hideModalDialog();
             };
         }
         else {
diff --git a/templates/main.html b/templates/main.html
index 8c981e5..e05fa39 100644
--- a/templates/main.html
+++ b/templates/main.html
@@ -32,6 +32,7 @@
         </div>
     </div>
     <div class="page">
+        {% if USER == 'admin' %}
         <div class="settings">
             <div class="settings-container">
                 <div class="settings-section-title"><input type="checkbox" class="styled section general" id="motionEyeSwitch">General Settings</div>
@@ -61,6 +62,19 @@
                         <td class="settings-item-value"><input type="password" class="styled general" id="normalPasswordEntry"></td>
                         <td><span class="help-mark" title="the password for the surveillance user">?</span></td>
                     </tr>
+                    <tr class="settings-item advanced-setting">
+                        <td colspan="100"><div class="settings-item-separator"></div></td>
+                    </tr>
+                    <tr class="settings-item advanced-setting">
+                        <td class="settings-item-label"><span class="settings-item-label">Current Version</span></td>
+                        <td class="settings-item-value"><span class="settings-item-label">{{VERSION}}</span></td>
+                        <td><span class="help-mark" title="checks for new versions and performs updates">?</span></td>
+                    </tr>
+                    <tr class="settings-item advanced-setting">
+                        <td class="settings-item-label"><span class="settings-item-label">Software Update</span></td>
+                        <td class="settings-item-value"><div class="button update-button" id="updateButton">Check</div></td>
+                        <td><span class="help-mark" title="checks for new versions and performs updates">?</span></td>
+                    </tr>
                 </table>
                 
                 <div class="settings-section-title"><input type="checkbox" class="styled section device" id="videoDeviceSwitch">Video Device</div>
@@ -418,6 +432,7 @@
                 </table>
             </div>
         </div>
+        {% endif %}
         <div class="page-container">
         </div>
         <div class="footer">