/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.core.application.update;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.jkiss.dbeaver.DBeaverPreferences;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.updater.VersionDescriptor;
import org.jkiss.dbeaver.utils.GeneralUtils;

import java.io.IOException;
import java.util.Calendar;

/**
 * Version checker job
 */
public class DBeaverVersionChecker extends AbstractJob {

    private static final Log log = Log.getLog(DBeaverVersionChecker.class);

    private final boolean showAlways;

    public DBeaverVersionChecker(boolean force)
    {
        super("DBeaver new version release checker");
        this.showAlways = force;
    }

    @Override
    protected IStatus run(DBRProgressMonitor monitor)
    {
        if (monitor.isCanceled() || DBeaverCore.isClosing()) {
            return Status.CANCEL_STATUS;
        }
        boolean showUpdateDialog = showAlways;
        if (!showUpdateDialog) {
            // Check for auto-update settings
            showUpdateDialog = DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.UI_AUTO_UPDATE_CHECK);
            if (showUpdateDialog) {

                long lastVersionCheckTime = DBeaverCore.getGlobalPreferenceStore().getLong(DBeaverPreferences.UI_UPDATE_CHECK_TIME);
                if (lastVersionCheckTime > 0) {
                    // Do not check more often than daily
                    Calendar cal = Calendar.getInstance();
                    cal.setTimeInMillis(lastVersionCheckTime);
                    int checkMonth = cal.get(Calendar.MONTH);
                    int checkDay = cal.get(Calendar.DAY_OF_MONTH);
                    cal.setTimeInMillis(System.currentTimeMillis());
                    int curMonth = cal.get(Calendar.MONTH);
                    int curDay = cal.get(Calendar.DAY_OF_MONTH);
                    if (curMonth == checkMonth && curDay == checkDay) {
                        // Already checked today
                        return Status.OK_STATUS;
                    }
                }
            }
        }
        if (!showAlways && !showUpdateDialog) {
            return Status.OK_STATUS;
        }
        try {
            DBeaverCore.getGlobalPreferenceStore().setValue(DBeaverPreferences.UI_UPDATE_CHECK_TIME, System.currentTimeMillis());
            final String updateURL = Platform.getProduct().getProperty("versionUpdateURL");
            if (updateURL == null) {
                return Status.OK_STATUS;
            }
            VersionDescriptor versionDescriptor = new VersionDescriptor(updateURL);

            if (versionDescriptor.getProgramVersion().compareTo(GeneralUtils.getProductVersion()) > 0) {
                showUpdaterDialog(versionDescriptor);
            } else if (showAlways) {
                showUpdaterDialog(null);
            }
        } catch (IOException e) {
            log.debug(e);
        }
        return Status.OK_STATUS;
    }

    private void showUpdaterDialog(final VersionDescriptor versionDescriptor)
    {
        DBeaverUI.asyncExec(new Runnable() {
            @Override
            public void run() {
                VersionUpdateDialog dialog = new VersionUpdateDialog(
                    DBeaverUI.getActiveWorkbenchShell(),
                    versionDescriptor);
                dialog.open();
            }
        });
    }
}

@@ -3107,26 +3107,9 @@
   %7D%0A
-        try %7B%0A    
+%0A
     
@@ -3238,20 +3238,16 @@
         
-    
 final St
@@ -3322,28 +3322,24 @@
 %22);%0A        
-    
 if (updateUR
@@ -3355,36 +3355,32 @@
 ) %7B%0A            
- 
-   
 return Status.OK
@@ -3388,36 +3388,32 @@
 STATUS;%0A        
-    
 %7D%0A            Ve
@@ -3402,28 +3402,24 @@
   %7D%0A        
- 
-   
 VersionDescr
@@ -3449,58 +3449,205 @@
  = n
-ew VersionDescriptor(updateURL);%0A%0A            if (
+ull;%0A        try %7B%0A            versionDescriptor = new VersionDescriptor(updateURL);%0A        %7D catch (IOException e) %7B%0A            log.debug(e);%0A        %7D%0A%0A        if (versionDescriptor != null && 
 vers
@@ -3735,36 +3735,32 @@
 ) %7B%0A            
-  
-  
 showUpdaterDialo
@@ -3789,20 +3789,16 @@
         
-    
 %7D else i
@@ -3818,36 +3818,32 @@
 ) %7B%0A            
-  
-  
 showUpdaterDialo
@@ -3863,83 +3863,10 @@
     
-    %7D%0A        %7D catch (IOException e) %7B%0A            log.debug(e);%0A        
 %7D
+%0A
 %0A   

Version update checker fix