[Title] common-eplugin: fixed the change problem of security profiles 01/11001/2
authorJihoon Song <jihoon80.song@samsung.com>
Wed, 16 Oct 2013 04:54:24 +0000 (13:54 +0900)
committerJihoon Song <jihoon80.song@samsung.com>
Wed, 16 Oct 2013 07:27:01 +0000 (16:27 +0900)
[Desc.]
[Issue]

Change-Id: I82ab5eac3aed1932a4d793290738171138ee0fd7

org.tizen.common.sign/META-INF/MANIFEST.MF
org.tizen.common.sign/src/org/tizen/common/sign/preferences/SigningPreferencePage.java
org.tizen.common.sign/src/org/tizen/common/sign/preferences/SigningProfileContainer.java
org.tizen.common.sign/src/org/tizen/common/sign/preferences/UIMessages.properties

index f7b3311..483634b 100644 (file)
@@ -10,7 +10,8 @@ Require-Bundle: org.eclipse.ui,
  org.eclipse.help
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
-Import-Package: org.eclipse.core.resources
+Import-Package: org.eclipse.core.resources,
+ org.eclipse.ui.actions
 Bundle-Vendor: %Bundle-Vendor
 Export-Package: hashsign,
  org.tizen.common.sign,
index c6a7001..004378f 100755 (executable)
@@ -30,13 +30,21 @@ import static org.tizen.common.util.StringUtil.EMPTY_STRING;
 import static org.tizen.common.util.StringUtil.isEmpty;
 import static org.tizen.common.util.StringUtil.trim;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import keycertificategenerator.TizenKeyCertificateGenerator;
 
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IncrementalProjectBuilder;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
@@ -48,6 +56,7 @@ import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.dialogs.IInputValidator;
 import org.eclipse.jface.dialogs.InputDialog;
 import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.preference.PreferencePage;
 import org.eclipse.jface.resource.JFaceResources;
@@ -80,6 +89,7 @@ import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.actions.WorkspaceModifyOperation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.tizen.common.TizenHelpContextIds;
@@ -94,6 +104,9 @@ import org.tizen.common.sign.util.SigningProfileUtil;
 import org.tizen.common.ui.dialog.FileDialogUtils;
 import org.tizen.common.util.FileUtil;
 import org.tizen.common.util.ObjectUtil;
+import org.tizen.common.util.ProjectUtil;
+import org.tizen.common.util.SWTUtil;
+import org.tizen.common.util.StringUtil;
 
 
 public class SigningPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
@@ -111,8 +124,66 @@ public class SigningPreferencePage extends PreferencePage implements IWorkbenchP
     protected List<Button> profilesEnableButton;
     
     protected SigningProfileContainer container;
+    protected SigningProfileContainer loadedContainer; // for modified check
     
-    
+    protected final WorkspaceModifyOperation projectCleanOperation = new WorkspaceModifyOperation() {
+        @Override
+        protected void execute(IProgressMonitor monitor)
+                throws CoreException, InvocationTargetException,
+                InterruptedException {
+            monitor = ObjectUtil.nvl( monitor, new NullProgressMonitor() );
+            
+            final int totalTick = 100;
+            try {
+                monitor.beginTask( "Clean projects : ", totalTick );
+                
+                // interact with an user for updating project signatures
+                IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
+                int cleanTick = totalTick / projects.length;
+                
+                boolean bAlways = false;
+                boolean bChoice = true;
+                for ( final IProject project : projects ) {
+                    monitor.subTask( project.getName() );
+                    
+                    // check a Tizen project
+                    if ( project.isAccessible() && ProjectUtil.isTizenProject( project ) ) {
+                        if ( ! bAlways ) {
+                            // interact with user
+                            int retValue = openQuestionDialogWithAll(
+                                    getShell(),
+                                    "Project clean",
+                                    UIMessages.getString( "org.tizen.common.sign.requiredclean" ) );
+                            
+                            switch ( retValue ) {
+                            case 1: // IDialogConstants.YES_TO_ALL_LABEL
+                                bAlways = true;
+                            case 0: // IDialogConstants.YES_LABEL
+                                bChoice = true;
+                                break;
+                            case 3: // IDialogConstants.NO_TO_ALL_LABEL
+                                bAlways = true;
+                            case 2: // IDialogConstants.NO_LABEL
+                                bChoice = false;
+                                break;
+                            }
+                        }
+                        
+                        if ( bChoice ) {
+                            // clean build
+                            // TODO: progress sub monitor cannot be used because thread invalid is occurred about CDT build.
+                            project.build( IncrementalProjectBuilder.CLEAN_BUILD, null );
+                        }
+                    }
+                    
+                    monitor.worked( cleanTick );
+                }
+            } finally {
+                monitor.done();
+            }
+        }
+    };
+        
     /**
      * Constructor
      */
@@ -136,6 +207,7 @@ public class SigningPreferencePage extends PreferencePage implements IWorkbenchP
         
         // load profile
         this.container = SigningProfileUtil.getProfileContainerFromFile();
+        this.loadedContainer = SigningProfileUtil.getProfileContainerFromFile();
         
         Composite composite = createComposite(
                 parent, SWT.NONE, createGridLayout( 1 ), new GridData( GridData.FILL_BOTH ) );
@@ -280,6 +352,7 @@ public class SigningPreferencePage extends PreferencePage implements IWorkbenchP
         String activeProfileName = SigningProfileUtil.getActiveProfileName();
         SigningProfile activeProfile = this.container.getProfileByName( activeProfileName );
         this.container.setActiveProfile( activeProfile );
+        this.loadedContainer.setActiveProfile( activeProfile );
         
         this.listViewer.setInput( "profile" );
         
@@ -894,8 +967,42 @@ public class SigningPreferencePage extends PreferencePage implements IWorkbenchP
 
     @Override
     public boolean performOk() {
-        SigningProfileUtil.setActiveProfileName( container.getActiveProfileName() );
-        SigningProfileUtil.writeProfilesToFile( container );
+        // check an active profile change
+        boolean bActiveChanged = false;
+        String newProfileName = container.getActiveProfileName();
+        if ( ! StringUtil.isEmpty( newProfileName ) &&
+                ! newProfileName.equals( loadedContainer.getActiveProfileName() ) ) {
+            bActiveChanged = true;
+        }
+        
+        // serialize for compare
+        ByteArrayOutputStream containerOs = new ByteArrayOutputStream();
+        container.writeProfileXML( containerOs );
+        ByteArrayOutputStream loadedContainerOs = new ByteArrayOutputStream();
+        loadedContainer.writeProfileXML( loadedContainerOs );
+        
+        // if information was modified
+        boolean bChanged = ! Arrays.equals( containerOs.toByteArray(), loadedContainerOs.toByteArray() );
+        if ( bActiveChanged || bChanged ) {
+                // write profiles
+                SigningProfileUtil.setActiveProfileName( newProfileName );
+                SigningProfileUtil.writeProfilesToFile( container );
+                logger.trace( "Security preferences was saved" );
+                
+                // update old comparable info to new info
+                loadedContainer.readProfileXML( new ByteArrayInputStream( containerOs.toByteArray() ) );
+                loadedContainer.setActiveProfile( loadedContainer.getProfileByName( newProfileName ) );
+            try {
+                ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog( getShell() );
+                monitorDialog.run( false, false, projectCleanOperation );
+            } catch (InvocationTargetException e) {
+                logger.error( e.getMessage(), e );
+                return false;
+            } catch (InterruptedException e) {
+                logger.error( e.getMessage(), e );
+                return false;
+            }
+        }
         
         return super.performOk();
     }
@@ -1028,6 +1135,28 @@ public class SigningPreferencePage extends PreferencePage implements IWorkbenchP
         return null;
     }
     
+    private int openQuestionDialogWithAll(final Shell shell, final String title, final String msg) {
+        final AtomicInteger retValue = new AtomicInteger();
+        
+        SWTUtil.syncExec( new Runnable() {
+            @Override
+            public void run() {
+                MessageDialog dialog = new MessageDialog(
+                        shell, title, null, msg, MessageDialog.QUESTION,
+                        new String[] {
+                                IDialogConstants.YES_LABEL,
+                                IDialogConstants.YES_TO_ALL_LABEL,
+                                IDialogConstants.NO_LABEL,
+                                IDialogConstants.NO_TO_ALL_LABEL },
+                        0);
+                dialog.setBlockOnOpen( true );
+                retValue.set( dialog.open() );
+            }
+        });
+        
+        return retValue.get();
+    }
+    
     /**
      * TextCellEditor for password processing
      */
index 519651e..d50c892 100644 (file)
@@ -101,8 +101,14 @@ public class SigningProfileContainer {
         }
         
         this.activeProfileName = defaultProfile.getProfileName();
-        
-        if ( ! this.profiles.contains( defaultProfile ) ) {
+        boolean bContain = false;
+        for ( SigningProfile profile : this.profiles ) {
+            if ( profile.getProfileName().equals( this.activeProfileName ) ) {
+                bContain = true;
+                break;
+            }
+        }
+        if ( ! bContain ) {
             addProfile( defaultProfile );
         }
     }
index 4513946..c64df77 100644 (file)
@@ -32,3 +32,4 @@ org.tizen.common.sign.certificatevaliditymessage2=is not valid.\nThe certificate
 org.tizen.common.sign.certificatevaliditymessage3=and after
 
 org.tizen.common.sign.note.certificate_generator=See <a>'Certificate Generator'</a> documentation to generate an author signature.
+org.tizen.common.sign.requiredclean=It is required to clean a project because profile information was changed.\nDo you want to do it now?
\ No newline at end of file