protected override void PerformExport(object settings)
        {
            var gpxSetting = settings as ExportGPXSettings;
            if (gpxSetting != null && !string.IsNullOrEmpty(gpxSetting.FileName))
            {
                bool canceled = false;
                try
                {
                    using (var db = new NPoco.Database(this.DatabaseConnection.Connection, NPoco.DatabaseType.SQLite))
                    {
                        double minLat = 0, minLon = 0, maxLat = 0, maxLon = 0;
                        var dr = DatabaseConnection.ExecuteReader(string.Format("select Min(Latitude), Max(Latitude), Min(Longitude), Max(Longitude) from Caches inner join {0} on Caches.Code={0}.gccode", ActionInputTableName));
                        if (dr.Read())
                        {
                            minLat = Utils.Conversion.StringToDouble(dr.GetString(0));
                            maxLat = Utils.Conversion.StringToDouble(dr.GetString(1));
                            minLon = Utils.Conversion.StringToDouble(dr.GetString(2));
                            maxLon = Utils.Conversion.StringToDouble(dr.GetString(3));
                        }
                        dr.Close();
                        var gcList = db.Fetch<string>(string.Format("select gccode from {0}", ActionInputTableName));
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ExportingGPX", "CreatingFile", gcList.Count, 0, true))
                        {
                            using (System.IO.TemporaryFile gpxFile = new System.IO.TemporaryFile(false))
                            {
                                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(gpxFile.Path, false, Encoding.UTF8))
                                {
                                    Utils.GPXGenerator gpxGenerator = new Utils.GPXGenerator(
                                        db
                                        , gcList
                                        , gpxSetting.Version
                                        , minLat
                                        , maxLat
                                        , minLon
                                        , maxLon
                                        );

                                    DateTime nextUpdate = DateTime.Now.AddSeconds(1);
                                    //generate header
                                    sw.Write(gpxGenerator.Start());
                                    //preserve mem and do for each cache the export
                                    for (int i = 0; i < gpxGenerator.Count; i++)
                                    {
                                        //write parent
                                        sw.WriteLine(gpxGenerator.Next());

                                        if (gpxSetting.AddChildWaypoints)
                                        {
                                            //write child waypoints
                                            string s = gpxGenerator.WaypointData();
                                            if (!string.IsNullOrEmpty(s))
                                            {
                                                sw.WriteLine(s);
                                            }
                                        }

                                        if (DateTime.Now >= nextUpdate)
                                        {
                                            if (!progress.Update("CreatingFile", gpxGenerator.Count, i + 1))
                                            {
                                                canceled = true;
                                                break;
                                            }
                                            nextUpdate = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                    //finalize
                                    sw.Write(gpxGenerator.Finish());
                                }

                                if (!canceled)
                                {
                                    if (gpxSetting.FileName.ToLower().EndsWith(".zip"))
                                    {
                                        using (FileStream zipToOpen = new FileStream(gpxSetting.FileName, FileMode.Create))
                                        {
                                            using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
                                            {
                                                ZipArchiveEntry gpxEntry = archive.CreateEntry("geocaches.gpx");
                                                using (StreamWriter writer = new StreamWriter(gpxEntry.Open()))
                                                {
                                                    writer.Write(File.ReadAllText(gpxFile.Path));
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        System.IO.File.Copy(gpxFile.Path, gpxSetting.FileName, true);
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }
Esempio n. 2
0
        protected override void ExportMethod()
        {
            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_EXPORTINGGPX, STR_CREATINGFILE, _gcList.Count, 0))
            {
                if (_oneGeocachePerFile)
                {
                    Utils.GPXGenerator gpxGenerator = new Utils.GPXGenerator(Core, _gcList, string.IsNullOrEmpty(PluginSettings.Instance.GPXVersionStr) ? Utils.GPXGenerator.V101 : Version.Parse(PluginSettings.Instance.GPXVersionStr));
                    gpxGenerator.UseNameForGCCode = _useName;
                    gpxGenerator.AddAdditionWaypointsToDescription = PluginSettings.Instance.AddWaypointsToDescription;
                    gpxGenerator.UseHintsForDescription            = PluginSettings.Instance.UseHintsForDescription;
                    gpxGenerator.AddFieldnotesToDescription        = PluginSettings.Instance.AddFieldNotesToDescription;
                    gpxGenerator.MaxNameLength             = PluginSettings.Instance.MaxGeocacheNameLength;
                    gpxGenerator.MinStartOfname            = PluginSettings.Instance.MinStartOfGeocacheName;
                    gpxGenerator.ExtraCoordPrefix          = PluginSettings.Instance.CorrectedNamePrefix;
                    gpxGenerator.AddExtraInfoToDescription = PluginSettings.Instance.AddExtraInfoToDescription;
                    gpxGenerator.MaxLogCount = PluginSettings.Instance.MaximumNumberOfLogs;
                    using (System.IO.TemporaryFile gpxFile = new System.IO.TemporaryFile(false))
                    {
                        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(gpxFile.Path, false, Encoding.UTF8))
                        {
                            int block = 0;
                            //generate header
                            sw.Write(gpxGenerator.Start());
                            //preserve mem and do for each cache the export
                            for (int i = 0; i < gpxGenerator.Count; i++)
                            {
                                sw.WriteLine(gpxGenerator.Next());
                                if (_addChildWaypoints)
                                {
                                    string s = gpxGenerator.WaypointData();
                                    if (!string.IsNullOrEmpty(s))
                                    {
                                        sw.WriteLine(s);
                                    }
                                }
                                block++;
                                if (block > 10)
                                {
                                    block = 0;
                                    progress.UpdateProgress(STR_EXPORTINGGPX, STR_CREATINGFILE, gpxGenerator.Count, i + 1);
                                }
                            }
                            //finalize
                            sw.Write(gpxGenerator.Finish());
                        }

                        progress.UpdateProgress(STR_EXPORTINGGPX, STR_COPYINGFILE, 1, 0);
                        string filename = "geocaches.gpx";
                        if (PluginSettings.Instance.UseDatabaseNameForFileName)
                        {
                            Framework.Interfaces.IPluginInternalStorage storage = (from Framework.Interfaces.IPluginInternalStorage a in Core.GetPlugin(Framework.PluginType.InternalStorage) select a).FirstOrDefault();
                            if (storage != null)
                            {
                                var si = storage.ActiveStorageDestination;
                                if (si != null)
                                {
                                    string s   = storage.ActiveStorageDestination.Name;
                                    int    pos = s.LastIndexOf('.');
                                    if (pos < 0)
                                    {
                                        filename = string.Format("{0}.gpx", s);
                                    }
                                    else
                                    {
                                        filename = string.Format("{0}.gpx", s.Substring(0, pos));
                                    }
                                }
                            }
                        }
                        System.IO.File.Copy(gpxFile.Path, System.IO.Path.Combine(new string[] { _drive, "garmin", "gpx", filename }), true);
                    }
                }
                else
                {
                    List <Framework.Data.Geocache> gcl = new List <Framework.Data.Geocache>();
                    for (int i = 0; i < _gcList.Count; i++)
                    {
                        gcl.Clear();
                        gcl.Add(_gcList[i]);
                        Utils.GPXGenerator gpxGenerator = new Utils.GPXGenerator(Core, gcl, string.IsNullOrEmpty(PluginSettings.Instance.GPXVersionStr) ? Utils.GPXGenerator.V101 : Version.Parse(PluginSettings.Instance.GPXVersionStr));
                        gpxGenerator.UseNameForGCCode = _useName;
                        gpxGenerator.AddAdditionWaypointsToDescription = PluginSettings.Instance.AddWaypointsToDescription;
                        gpxGenerator.UseHintsForDescription            = PluginSettings.Instance.UseHintsForDescription;
                        gpxGenerator.AddFieldnotesToDescription        = PluginSettings.Instance.AddFieldNotesToDescription;
                        gpxGenerator.MaxNameLength             = PluginSettings.Instance.MaxGeocacheNameLength;
                        gpxGenerator.MinStartOfname            = PluginSettings.Instance.MinStartOfGeocacheName;
                        gpxGenerator.ExtraCoordPrefix          = PluginSettings.Instance.CorrectedNamePrefix;
                        gpxGenerator.AddExtraInfoToDescription = PluginSettings.Instance.AddExtraInfoToDescription;
                        gpxGenerator.MaxLogCount = PluginSettings.Instance.MaximumNumberOfLogs;
                        using (System.IO.TemporaryFile gpxFile = new System.IO.TemporaryFile(true))
                        {
                            int block = 0;

                            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(gpxFile.Path, false, Encoding.ASCII))
                            {
                                //generate header
                                sw.Write(gpxGenerator.Start());
                                //preserve mem and do for each cache the export
                                sw.WriteLine(gpxGenerator.Next());
                                if (_addChildWaypoints)
                                {
                                    string s = gpxGenerator.WaypointData();
                                    if (!string.IsNullOrEmpty(s))
                                    {
                                        sw.WriteLine(s);
                                    }
                                }
                                //finalize
                                sw.Write(gpxGenerator.Finish());
                            }
                            System.IO.File.Copy(gpxFile.Path, System.IO.Path.Combine(new string[] { _drive, "garmin", "gpx", string.Format("{0}.gpx", gcl[0].Code) }), true);

                            block++;
                            if (block > 10)
                            {
                                block = 0;
                                progress.UpdateProgress(STR_EXPORTINGGPX, STR_CREATINGFILE, _gcList.Count, i + 1);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Visible)
            {
                try
                {
                    bool ready = (bool)executeScript("isReady", null);
                    if (ready)
                    {
                        if (_running)
                        {
                            if (_nextIndex < _gcList.Count)
                            {
                                List<Framework.Data.Geocache> gcList = new List<Framework.Data.Geocache>();
                                gcList.Add(_gcList[_nextIndex]);

                                Utils.GPXGenerator gpxGenerator = new Utils.GPXGenerator(_core, gcList, string.IsNullOrEmpty(Properties.Settings.Default.GPXVersionStr) ? Utils.GPXGenerator.V101 : Version.Parse(Properties.Settings.Default.GPXVersionStr));
                                gpxGenerator.AddFieldnotesToDescription = Properties.Settings.Default.AddFieldNotesToDescription;
                                gpxGenerator.MaxNameLength = Properties.Settings.Default.MaxGeocacheNameLength;
                                gpxGenerator.MinStartOfname = Properties.Settings.Default.MinStartOfGeocacheName;
                                gpxGenerator.UseNameForGCCode = Properties.Settings.Default.UseNameAndNotCode;
                                gpxGenerator.AddAdditionWaypointsToDescription = Properties.Settings.Default.AddWaypointsToDescription;
                                gpxGenerator.UseHintsForDescription = Properties.Settings.Default.UseHintsForDescription;
                                gpxGenerator.ExtraCoordPrefix = Properties.Settings.Default.CorrectedNamePrefix;
                                
                                using (System.IO.TemporaryFile gpxFile = new System.IO.TemporaryFile(true))
                                {
                                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(gpxFile.Path, false, Encoding.UTF8))
                                    {
                                        int block = 0;
                                        //generate header
                                        sw.Write(gpxGenerator.Start());
                                        //preserve mem and do for each cache the export
                                        for (int i = 0; i < gpxGenerator.Count; i++)
                                        {
                                            sw.WriteLine(gpxGenerator.Next());
                                            if (Properties.Settings.Default.AddChildWaypoints)
                                            {
                                                string s = gpxGenerator.WaypointData();
                                                if (!string.IsNullOrEmpty(s))
                                                {
                                                    sw.WriteLine(s);
                                                }
                                            }
                                            block++;
                                            if (block > 10)
                                            {
                                                block = 0;
                                            }
                                        }
                                        //finalize
                                        sw.Write(gpxGenerator.Finish());
                                    }
                                    executeScript("uploadGpx", new object[] { System.IO.File.ReadAllText(gpxFile.Path), string.Format("{0}.gpx", gcList[0].Code) });
                                }
                                _nextIndex++;
                                toolStripStatusLabel1.Text = string.Format("{0}/{1}", _nextIndex, _gcList.Count);
                                toolStripProgressBar1.Value = _nextIndex;
                            }
                            else
                            {
                                //done, close
                                timer1.Enabled = false;
                                Close();
                            }
                        }
                        else
                        {
                            button1.Enabled = true;
                        }
                    }
                }
                catch
                {
                }
            }
            else
            {
                timer1.Enabled = false;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Visible)
            {
                try
                {
                    bool ready = (bool)executeScript("isReady", null);
                    if (ready)
                    {
                        if (_running)
                        {
                            if (_nextIndex < _gcList.Count)
                            {
                                List <Framework.Data.Geocache> gcList = new List <Framework.Data.Geocache>();
                                gcList.Add(_gcList[_nextIndex]);

                                Utils.GPXGenerator gpxGenerator = new Utils.GPXGenerator(_core, gcList, string.IsNullOrEmpty(PluginSettings.Instance.GPXVersionStr) ? Utils.GPXGenerator.V101 : Version.Parse(PluginSettings.Instance.GPXVersionStr));
                                gpxGenerator.AddFieldnotesToDescription = PluginSettings.Instance.AddFieldNotesToDescription;
                                gpxGenerator.MaxNameLength    = PluginSettings.Instance.MaxGeocacheNameLength;
                                gpxGenerator.MinStartOfname   = PluginSettings.Instance.MinStartOfGeocacheName;
                                gpxGenerator.UseNameForGCCode = PluginSettings.Instance.UseNameAndNotCode;
                                gpxGenerator.AddAdditionWaypointsToDescription = PluginSettings.Instance.AddWaypointsToDescription;
                                gpxGenerator.UseHintsForDescription            = PluginSettings.Instance.UseHintsForDescription;
                                gpxGenerator.ExtraCoordPrefix          = PluginSettings.Instance.CorrectedNamePrefix;
                                gpxGenerator.AddExtraInfoToDescription = PluginSettings.Instance.AddExtraInfoToDescription;
                                gpxGenerator.MaxLogCount = PluginSettings.Instance.MaximumNumberOfLogs;

                                using (System.IO.TemporaryFile gpxFile = new System.IO.TemporaryFile(true))
                                {
                                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(gpxFile.Path, false, Encoding.UTF8))
                                    {
                                        int block = 0;
                                        //generate header
                                        sw.Write(gpxGenerator.Start());
                                        //preserve mem and do for each cache the export
                                        for (int i = 0; i < gpxGenerator.Count; i++)
                                        {
                                            sw.WriteLine(gpxGenerator.Next());
                                            if (PluginSettings.Instance.AddChildWaypoints)
                                            {
                                                string s = gpxGenerator.WaypointData();
                                                if (!string.IsNullOrEmpty(s))
                                                {
                                                    sw.WriteLine(s);
                                                }
                                            }
                                            block++;
                                            if (block > 10)
                                            {
                                                block = 0;
                                            }
                                        }
                                        //finalize
                                        sw.Write(gpxGenerator.Finish());
                                    }
                                    executeScript("uploadGpx", new object[] { System.IO.File.ReadAllText(gpxFile.Path), string.Format("{0}.gpx", gcList[0].Code) });
                                }
                                _nextIndex++;
                                toolStripStatusLabel1.Text  = string.Format("{0}/{1}", _nextIndex, _gcList.Count);
                                toolStripProgressBar1.Value = _nextIndex;
                            }
                            else
                            {
                                //done, close
                                timer1.Enabled = false;
                                Close();
                            }
                        }
                        else
                        {
                            button1.Enabled = true;
                        }
                    }
                }
                catch
                {
                }
            }
            else
            {
                timer1.Enabled = false;
            }
        }
Esempio n. 5
0
        protected override void ExportMethod()
        {
            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_EXPORTINGGPX, STR_CREATINGFILE, _gcList.Count, 0))
            {
                if (_oneGeocachePerFile)
                {
                    Utils.GPXGenerator gpxGenerator = new Utils.GPXGenerator(Core, _gcList, string.IsNullOrEmpty(PluginSettings.Instance.GPXVersionStr) ? Utils.GPXGenerator.V101 : Version.Parse(PluginSettings.Instance.GPXVersionStr));
                    gpxGenerator.UseNameForGCCode = _useName;
                    gpxGenerator.AddAdditionWaypointsToDescription = PluginSettings.Instance.AddWaypointsToDescription;
                    gpxGenerator.UseHintsForDescription = PluginSettings.Instance.UseHintsForDescription;
                    gpxGenerator.AddFieldnotesToDescription = PluginSettings.Instance.AddFieldNotesToDescription;
                    gpxGenerator.MaxNameLength = PluginSettings.Instance.MaxGeocacheNameLength;
                    gpxGenerator.MinStartOfname = PluginSettings.Instance.MinStartOfGeocacheName;
                    gpxGenerator.ExtraCoordPrefix = PluginSettings.Instance.CorrectedNamePrefix;
                    gpxGenerator.AddExtraInfoToDescription = PluginSettings.Instance.AddExtraInfoToDescription;
                    gpxGenerator.MaxLogCount = PluginSettings.Instance.MaximumNumberOfLogs;
                    using (System.IO.TemporaryFile gpxFile = new System.IO.TemporaryFile(false))
                    {
                        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(gpxFile.Path, false, Encoding.UTF8))
                        {
                            int block = 0;
                            //generate header
                            sw.Write(gpxGenerator.Start());
                            //preserve mem and do for each cache the export
                            for (int i = 0; i < gpxGenerator.Count; i++)
                            {
                                sw.WriteLine(gpxGenerator.Next());
                                if (_addChildWaypoints)
                                {
                                    string s = gpxGenerator.WaypointData();
                                    if (!string.IsNullOrEmpty(s))
                                    {
                                        sw.WriteLine(s);
                                    }
                                }
                                block++;
                                if (block > 10)
                                {
                                    block = 0;
                                    progress.UpdateProgress(STR_EXPORTINGGPX, STR_CREATINGFILE, gpxGenerator.Count, i + 1);
                                }
                            }
                            //finalize
                            sw.Write(gpxGenerator.Finish());
                        }

                        progress.UpdateProgress(STR_EXPORTINGGPX, STR_COPYINGFILE, 1, 0);
                        string filename = "geocaches.gpx";
                        if (PluginSettings.Instance.UseDatabaseNameForFileName)
                        {
                            Framework.Interfaces.IPluginInternalStorage storage = (from Framework.Interfaces.IPluginInternalStorage a in Core.GetPlugin(Framework.PluginType.InternalStorage) select a).FirstOrDefault();
                            if (storage!=null)
                            {
                                var si = storage.ActiveStorageDestination;
                                if (si != null)
                                {
                                    string s = storage.ActiveStorageDestination.Name;
                                    int pos = s.LastIndexOf('.');
                                    if (pos < 0)
                                    {
                                        filename = string.Format("{0}.gpx",s);
                                    }
                                    else
                                    {
                                        filename = string.Format("{0}.gpx",s.Substring(0,pos));
                                    }
                                }
                            }
                        }
                        System.IO.File.Copy(gpxFile.Path, System.IO.Path.Combine(new string[] { _drive, "garmin", "gpx", filename }), true);
                    }
                }
                else
                {
                    List<Framework.Data.Geocache> gcl = new List<Framework.Data.Geocache>();
                    for (int i = 0; i < _gcList.Count; i++)
                    {
                        gcl.Clear();
                        gcl.Add(_gcList[i]);
                        Utils.GPXGenerator gpxGenerator = new Utils.GPXGenerator(Core, gcl, string.IsNullOrEmpty(PluginSettings.Instance.GPXVersionStr) ? Utils.GPXGenerator.V101 : Version.Parse(PluginSettings.Instance.GPXVersionStr));
                        gpxGenerator.UseNameForGCCode = _useName;
                        gpxGenerator.AddAdditionWaypointsToDescription = PluginSettings.Instance.AddWaypointsToDescription;
                        gpxGenerator.UseHintsForDescription = PluginSettings.Instance.UseHintsForDescription;
                        gpxGenerator.AddFieldnotesToDescription = PluginSettings.Instance.AddFieldNotesToDescription;
                        gpxGenerator.MaxNameLength = PluginSettings.Instance.MaxGeocacheNameLength;
                        gpxGenerator.MinStartOfname = PluginSettings.Instance.MinStartOfGeocacheName;
                        gpxGenerator.ExtraCoordPrefix = PluginSettings.Instance.CorrectedNamePrefix;
                        gpxGenerator.AddExtraInfoToDescription = PluginSettings.Instance.AddExtraInfoToDescription;
                        gpxGenerator.MaxLogCount = PluginSettings.Instance.MaximumNumberOfLogs;
                        using (System.IO.TemporaryFile gpxFile = new System.IO.TemporaryFile(true))
                        {
                            int block = 0;

                            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(gpxFile.Path, false, Encoding.ASCII))
                            {
                                //generate header
                                sw.Write(gpxGenerator.Start());
                                //preserve mem and do for each cache the export
                                sw.WriteLine(gpxGenerator.Next());
                                if (_addChildWaypoints)
                                {
                                    string s = gpxGenerator.WaypointData();
                                    if (!string.IsNullOrEmpty(s))
                                    {
                                        sw.WriteLine(s);
                                    }
                                }
                                //finalize
                                sw.Write(gpxGenerator.Finish());
                            }
                            System.IO.File.Copy(gpxFile.Path, System.IO.Path.Combine(new string[] { _drive, "garmin", "gpx", string.Format("{0}.gpx", gcl[0].Code) }), true);

                            block++;
                            if (block > 10)
                            {
                                block = 0;
                                progress.UpdateProgress(STR_EXPORTINGGPX, STR_CREATINGFILE, _gcList.Count, i + 1);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        protected override void PerformExport(object settings)
        {
            var gpxSetting = settings as ExportGPXSettings;

            if (gpxSetting != null && !string.IsNullOrEmpty(gpxSetting.FileName))
            {
                bool canceled = false;
                try
                {
                    using (var db = new NPoco.Database(this.DatabaseConnection.Connection, NPoco.DatabaseType.SQLite))
                    {
                        double minLat = 0, minLon = 0, maxLat = 0, maxLon = 0;
                        var    dr = DatabaseConnection.ExecuteReader(string.Format("select Min(Latitude), Max(Latitude), Min(Longitude), Max(Longitude) from Caches inner join {0} on Caches.Code={0}.gccode", ActionInputTableName));
                        if (dr.Read())
                        {
                            minLat = Utils.Conversion.StringToDouble(dr.GetString(0));
                            maxLat = Utils.Conversion.StringToDouble(dr.GetString(1));
                            minLon = Utils.Conversion.StringToDouble(dr.GetString(2));
                            maxLon = Utils.Conversion.StringToDouble(dr.GetString(3));
                        }
                        dr.Close();
                        var gcList = db.Fetch <string>(string.Format("select gccode from {0}", ActionInputTableName));
                        using (Utils.ProgressBlock progress = new Utils.ProgressBlock("ExportingGPX", "CreatingFile", gcList.Count, 0, true))
                        {
                            using (System.IO.TemporaryFile gpxFile = new System.IO.TemporaryFile(false))
                            {
                                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(gpxFile.Path, false, Encoding.UTF8))
                                {
                                    Utils.GPXGenerator gpxGenerator = new Utils.GPXGenerator(
                                        db
                                        , gcList
                                        , gpxSetting.Version
                                        , minLat
                                        , maxLat
                                        , minLon
                                        , maxLon
                                        );

                                    DateTime nextUpdate = DateTime.Now.AddSeconds(1);
                                    //generate header
                                    sw.Write(gpxGenerator.Start());
                                    //preserve mem and do for each cache the export
                                    for (int i = 0; i < gpxGenerator.Count; i++)
                                    {
                                        //write parent
                                        sw.WriteLine(gpxGenerator.Next());

                                        if (gpxSetting.AddChildWaypoints)
                                        {
                                            //write child waypoints
                                            string s = gpxGenerator.WaypointData();
                                            if (!string.IsNullOrEmpty(s))
                                            {
                                                sw.WriteLine(s);
                                            }
                                        }

                                        if (DateTime.Now >= nextUpdate)
                                        {
                                            if (!progress.Update("CreatingFile", gpxGenerator.Count, i + 1))
                                            {
                                                canceled = true;
                                                break;
                                            }
                                            nextUpdate = DateTime.Now.AddSeconds(1);
                                        }
                                    }
                                    //finalize
                                    sw.Write(gpxGenerator.Finish());
                                }

                                if (!canceled)
                                {
                                    if (gpxSetting.FileName.ToLower().EndsWith(".zip"))
                                    {
                                        using (FileStream zipToOpen = new FileStream(gpxSetting.FileName, FileMode.Create))
                                        {
                                            using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
                                            {
                                                ZipArchiveEntry gpxEntry = archive.CreateEntry("geocaches.gpx");
                                                using (StreamWriter writer = new StreamWriter(gpxEntry.Open()))
                                                {
                                                    writer.Write(File.ReadAllText(gpxFile.Path));
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        System.IO.File.Copy(gpxFile.Path, gpxSetting.FileName, true);
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }
Esempio n. 7
0
        protected override void ExportMethod()
        {
            string gpxFile;
            string wptFile;

            System.IO.TemporaryFile tmp    = null;
            System.IO.TemporaryFile tmpwpt = null;
            if (PluginSettings.Instance.ZipFile)
            {
                tmp     = new System.IO.TemporaryFile(false);
                gpxFile = tmp.Path;
                tmpwpt  = new System.IO.TemporaryFile(false);
                wptFile = tmpwpt.Path;
            }
            else
            {
                gpxFile = _filename;
                wptFile = string.Format("{0}-wpts.gpx", gpxFile.Substring(0, gpxFile.Length - 4));
            }
            using (Utils.ProgressBlock progress = new Utils.ProgressBlock(this, STR_EXPORTINGGPX, STR_CREATINGFILE, _gpxGenerator.Count, 0))
            {
                DateTime nextUpdate = DateTime.Now.AddSeconds(2);
                //create file stream (if not zipped actual file and if zipped tmp file
                using (System.IO.StreamWriter sw = System.IO.File.CreateText(gpxFile))
                    using (System.IO.StreamWriter swwp = System.IO.File.CreateText(wptFile))
                    {
                        //generate header
                        sw.Write(_gpxGenerator.Start());
                        if (PluginSettings.Instance.AddWaypoints)
                        {
                            swwp.Write(_gpxGenerator.WaypointData());
                        }
                        //preserve mem and do for each cache the export
                        for (int i = 0; i < _gpxGenerator.Count; i++)
                        {
                            sw.WriteLine(_gpxGenerator.Next());
                            if (PluginSettings.Instance.AddWaypoints)
                            {
                                string s = _gpxGenerator.WaypointData();
                                if (!string.IsNullOrEmpty(s))
                                {
                                    swwp.WriteLine(s);
                                }
                            }
                            if (DateTime.Now >= nextUpdate)
                            {
                                progress.UpdateProgress(STR_EXPORTINGGPX, STR_CREATINGFILE, _gpxGenerator.Count, i + 1);
                                nextUpdate = DateTime.Now.AddSeconds(2);
                            }
                        }
                        //finalize
                        sw.Write(_gpxGenerator.Finish());
                        if (PluginSettings.Instance.AddWaypoints)
                        {
                            swwp.Write(_gpxGenerator.Finish());
                        }
                    }

                if (PluginSettings.Instance.ZipFile)
                {
                    try
                    {
                        List <string> filenames = new List <string>();
                        filenames.Add(gpxFile);
                        if (PluginSettings.Instance.AddWaypoints)
                        {
                            filenames.Add(wptFile);
                        }

                        using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(_filename)))
                        {
                            s.SetLevel(9); // 0-9, 9 being the highest compression

                            byte[] buffer = new byte[4096];
                            bool   wpt    = false;

                            foreach (string file in filenames)
                            {
                                ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(wpt ? _filename.ToLower().Replace(".zip", "-wpts.gpx") : _filename.ToLower().Replace(".zip", ".gpx")));

                                entry.DateTime = DateTime.Now;
                                s.PutNextEntry(entry);

                                using (System.IO.FileStream fs = System.IO.File.OpenRead(file))
                                {
                                    int sourceBytes;
                                    do
                                    {
                                        sourceBytes = fs.Read(buffer, 0,
                                                              buffer.Length);

                                        s.Write(buffer, 0, sourceBytes);
                                    } while (sourceBytes > 0);
                                }

                                wpt = true;
                            }
                            s.Finish();
                            s.Close();
                        }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    if (!PluginSettings.Instance.AddWaypoints)
                    {
                        System.IO.File.Delete(wptFile);
                    }
                }
            }
            if (tmp != null)
            {
                tmp.Dispose();
                tmp = null;
            }
            if (tmpwpt != null)
            {
                tmpwpt.Dispose();
                tmpwpt = null;
            }
        }