/// <summary>
        /// project.count = 1 : return new ProjectConfigration
        /// project.count > 1 : return next.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public ProjectConfigration deleteProject(int index)
        {
            if (projects.Count < 0 || index < 0 || (index + 1 > projects.Count))
            {
                throw new Exception("index out of range");
            }

            string project_name         = projects.Keys[index];
            string setting_project_path = Path.Combine(System.Environment.CurrentDirectory,
                                                       Path.Combine("projects", Utils.generateSettingFileName(project_name)));


            if (File.Exists(setting_project_path))
            {
                File.Delete(setting_project_path);
            }

            projects.RemoveAt(index);

            ProjectConfigration project = null;

            if (projects.Count <= 0)
            {
                project = new ProjectConfigration();
            }
            else
            {
                project = projects.Values[0];
            }

            return(project);
        }
        private void LoadProjects()
        {
            if (!Directory.Exists(projects_path))
            {
                return;
            }

            string []           files        = Directory.GetFiles(projects_path);
            string              project_name = null;
            ProjectConfigration pconfig      = null;

            foreach (string f in files.Where(X => ((string)X).Contains("setting@")))
            {
                project_name = parceProjectNameFromFileName(f);
                pconfig      = ProjectConfigration.readSettingFromFile(project_name);

                if (pconfig == null)
                {
                    File.Delete(f);
                }
                else
                {
                    projects.Add(project_name, pconfig);
                }
            }
        }
        public ProjectConfigration getOrCreateProject(string project)
        {
            ProjectConfigration config = null;

            if (projects.ContainsKey(project))
            {
                config = projects.Values[projects.IndexOfKey(project)];
            }
            if (config == null)
            {
                config = new ProjectConfigration();
            }


            return(config);
        }
        public ProjectConfigration getDefaultProject()
        {
            ProjectConfigration config = null;

            if (projects != null && projects.Count > 0)
            {
                config = projects.Values[0] as ProjectConfigration;
            }

            if (config == null)
            {
                config = new ProjectConfigration();
            }

            return(config);
        }
        /// <summary>
        /// if the new project exits, return and checkout the old project
        /// else if the generate a new project!
        /// </summary>
        /// <param name="new_project"></param>
        /// <returns></returns>
        public ProjectConfigration addProject(string new_project)
        {
            currentProject = new_project;

            if (projects.ContainsKey(new_project))
            {
                return(getCurrentProjectConfig());
            }

            ProjectConfigration project = new ProjectConfigration();

            project.writeSettintToFile(new_project);
            projects.Add(new_project, project);

            return(project);
        }
        //private string currentProject = null;
        // A simple analytics sdk for windows form
        //MobclickAgent agent = MobclickAgent.Instance();
        public MainForm()
        {
            Application.ApplicationExit += new EventHandler(this.Application_ApplicationExit);

            InitializeComponent();

            refreshProjects();

            project = Configration.Instanse().getDefaultProject();

            bindProjectConfig();
            bindGeneralConfig();

            initBackgroundWorker();

            //agent.StartNewSession("50596b7c52701557f6000157", "official");
        }
 public void saveCurrentProject(ProjectConfigration project)
 {
     project.writeSettintToFile(currentProject);
 }
        public bool isEnviromentReady(ProjectConfigration project)
        {
            string error = null;
            if(project.project_path == null){
                Log.e("Please set the project path");
                error = "工程目录没有设置";

            }

            if (project.isApkProject)
            {
                if (!File.Exists(project.project_path))
                {
                    Log.e("input apk doesn't exit");
                    error = "指定 APK 文件不存在";
                }
                else
                {
                    Log.i("Target apk is OK ... ");
                }
            }
            else
            {
                if (!Directory.Exists(project.project_path))
                {
                    Log.e("The input project path does't exit");
                    error = "工程目录不存在";

                }
                else
                {
                    Log.i("Target project is OK ... ");
                }
            }

            if(project.keystore_file_path == null){
                Log.e("Please set the keystore file path");
                error = "密钥文件没设置";

            }
            if(!File.Exists(project.keystore_file_path)){
                Log.e("The input keystore file doesn't exit");
                error = "密钥文件不存在";

            }else{
                Log.i("Target keystore file is OK ...");
            }

            if(project.keystore_pw == null){
                error = "密钥库密码没设置";
                Log.e("The input keystore password is null");

            }

            if(project.key_pw == null){
                error = "密钥密码没设置";
                Log.e("The input key password is null");

            }

            if(project.alias == null){
                error = "密钥别名没有设置";
                Log.e("The input alias is null");

            }

            if(project.channels == null || project.channels.Count <=0){
                error = "渠道没有设置";
                Log.w("Please input channels !");

            }

            if(string.IsNullOrEmpty(Configration.Instanse().java_home))
            {
                error = "JDK 路径没有设置";
            }

            if(string.IsNullOrEmpty(Configration.Instanse().android_home))
            {
                error = "Android SDK 路径没有设置";
            }

            if(error != null)
            {
                MessageBox.Show(error);
                return false;
            }

            Configration.Instanse().setEnvironment();

            //if(android_sdk_path == null)
            //if(java sdk path !
            return true;
        }
 public static void setProject(ProjectConfigration p, System.ComponentModel.BackgroundWorker bw)
 {
     project      = p;
     workReporter = bw;
 }
        private void OnGenerateProject(string path)
        {
            string new_project = System.IO.Path.GetFileName( path );
            project = Configration.Instanse().addProject(new_project);
            project.project_path = path;

            if (path.ToLower().EndsWith(".apk"))
            {
                project.isApkProject = true;
            }
            refreshProjects();
            bindProjectConfig();

            agent.OnEvent("build", "new_project");
        }
        private void deleteProject(int index)
        {
            if(index < 0) return;
            try{
                project = Configration.Instanse().deleteProject(index);
            }catch{}

            refreshProjects();
            bindProjectConfig();
        }
 public void saveCurrentProject(ProjectConfigration project)
 {
     project.writeSettintToFile(currentProject);
 }
        public ProjectConfigration getOrCreateProject(string project)
        {
            ProjectConfigration config = null;
            if(projects.ContainsKey(project)){
                config = projects.Values[projects.IndexOfKey(project)];
            }
            if(config == null) {
                config = new ProjectConfigration();
            }

            return config;
        }
        public ProjectConfigration getDefaultProject()
        {
            ProjectConfigration config = null;
            if(projects!= null && projects.Count > 0){
                config = projects.Values[0] as ProjectConfigration;
            }

            if(config == null) config = new ProjectConfigration();

            return config;
        }
        /// <summary>
        /// project.count = 1 : return new ProjectConfigration
        /// project.count > 1 : return next.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public ProjectConfigration deleteProject(int index)
        {
            if(projects.Count < 0 || index < 0 || (index+1 > projects.Count))
            {
                throw new Exception("index out of range");
            }

            string project_name  = projects.Keys[index];
            string setting_project_path = Path.Combine(System.Environment.CurrentDirectory,
                Path.Combine("projects", Utils.generateSettingFileName(project_name)));

            if( File.Exists(setting_project_path))  File.Delete(setting_project_path);

            projects.RemoveAt(index);

            ProjectConfigration project = null;

            if(projects.Count <= 0)
            {
                project = new ProjectConfigration();
            }else
            {
                project = projects.Values[0];
            }

            return project;
        }
        /// <summary>
        /// if the new project exits, return and checkout the old project
        /// else if the generate a new project!
        /// </summary>
        /// <param name="new_project"></param>
        /// <returns></returns>
        public ProjectConfigration addProject(string new_project)
        {
            currentProject = new_project;

            if(projects.ContainsKey(new_project)){
                return getCurrentProjectConfig();
            }

            ProjectConfigration project =  new ProjectConfigration();
            project.writeSettintToFile(new_project);
            projects.Add(new_project, project);

            return project;
        }
 public static void setProject(ProjectConfigration p, System.ComponentModel.BackgroundWorker bw)
 {
     project = p;
     workReporter = bw;
 }
        void ProjectsSelectedIndexChanged(object sender, EventArgs e)
        {
            if(projects.SelectedIndex < 0) return;

            string project_name = projects.Items[projects.SelectedIndex] as string;

            project = Configration.Instanse().updateCurrentProject(project_name);

            bindProjectConfig();
        }