示例#1
0
        private static TrackingDevice _FindDevice(
            IEnumerable<TrackingDevice> devices,
            MobileDevice device)
        {
            foreach (TrackingDevice td in devices)
            {
                if (td.Device.Equals(device))
                    return td;
            }

            return null;
        }
        ///////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            MobileDevice obj = new MobileDevice();
            this.CopyTo(obj);

            return obj;
        }
        /// <summary>
        /// Begins editing of the specified device.
        /// </summary>
        /// <param name="device">Begins editing of the specified mobile device.</param>
        /// <exception cref="ArgumentNullException"><paramref name="device"/> is a null reference.
        /// </exception>
        internal void BeginEditing(MobileDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            _editingDevice = device;
            _currentSyncType = device.SyncType;
            _currentTrackingId = device.TrackingId;
        }
        /// <summary>
        /// Finishes editing of the device being edited currently. Should be called only after
        /// the <see cref="BeginEditing"/> method to finish editing.
        /// </summary>
        internal void FinishEditing()
        {
            // Clean up reference to the currently being edited device.
            var editingDevice = _editingDevice;
            _editingDevice = null;

            if (editingDevice == null)
            {
                // No device is being edited now, so nothing do.
                return;
            }

            // Device is not on tracking service, either it was deleted or it was never there.
            if (editingDevice.SyncType != SyncType.WMServer)
            {
                if (_currentSyncType != SyncType.WMServer)
                {
                    // Device was never a tracking one.
                    return;
                }

                // Device was on the tracking service, so we need to delete it.
                var trackingIds = EnumerableEx.Return(_currentTrackingId);
                _ExecuteEditingOperation(
                    Properties.Resources.DevicesDeletionStatus,
                    () => _synchronizationService.DeleteDevices(_project, trackingIds));

                return;
            }

            if (!editingDevice.IsValid)
            {
                // Nothing to do with invalid device.
                return;
            }

            // Device is on the tracking service now, either it was added or updated.
            Debug.Assert(editingDevice.SyncType == SyncType.WMServer);

            if (_currentSyncType == editingDevice.SyncType &&
                _currentTrackingId == editingDevice.TrackingId)
            {
                // No changes in tracking-related properties.
                return;
            }

            if (_currentSyncType != SyncType.WMServer || string.IsNullOrEmpty(_currentTrackingId))
            {
                // A new device was added.
                this.AddDevice(editingDevice);

                return;
            }

            // TrackingId was changed.
            var devices = new Dictionary<string, string>();
            devices[_currentTrackingId] = editingDevice.TrackingId;

            _ExecuteEditingOperation(
                Properties.Resources.DevicesUpdateStatus,
                () => _synchronizationService.UpdateTrackingIds(devices));
        }
        /// <summary>
        /// Adds the specified device to the tracking service if device synchronization type
        /// is <see cref="SyncType.WMServer"/>.
        /// </summary>
        /// <param name="device">The reference to the device to be added.</param>
        /// <exception cref="ArgumentNullException"><paramref name="device"/> is a null reference.
        /// </exception>
        internal void AddDevice(MobileDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("devices");
            }

            if (device.SyncType != SyncType.WMServer)
            {
                return;
            }

            var trackingIds = EnumerableEx.Return(device.TrackingId);
            _ExecuteEditingOperation(
                Properties.Resources.DevicesAdditionStatus,
                () => _synchronizationService.AddDevices(trackingIds));
        }
        /// <summary>
        /// Save GRF file to folder
        /// </summary>
        /// <param name="route">Route to export</param>
        /// <param name="filename">GRF Filename</param>
        /// <param name="selectedPropertyNames">Names of selected properties</param>
        /// <param name="mobileDevice">Mobile device</param>
        private GrfExportResult _SendToFolder(Route route, string filename, IList<string> selectedPropertyNames, MobileDevice mobileDevice)
        {
            if (string.IsNullOrEmpty(mobileDevice.SyncFolder))
                throw new SettingsException( Properties.Resources.FolderPathEmpty);

            string filePath = Path.Combine(mobileDevice.SyncFolder, filename);

            return GrfExporter.ExportToGRF(filePath, route, _app.Project, _app.Geocoder, _app.Solver,
                selectedPropertyNames, _grfExporterConfig.RouteGrfCompression);
        }
        /// <summary>
        /// Send GRF by E-Mail
        /// </summary>
        /// <param name="route">Route to export</param>
        /// <param name="filename">GRF Filename</param>
        /// <param name="selectedPropertyNames">Names of selected properties</param>
        /// <param name="mobileDevice">Mobile device</param>
        private GrfExportResult _SendToEMail(Route route, string filename, IList<string> selectedPropertyNames, MobileDevice mobileDevice)
        {
            string filePath = "";
            try
            {
                if (string.IsNullOrEmpty(mobileDevice.EmailAddress))
                    throw new SettingsException(Properties.Resources.MailAddressEmpty);

                string tempFolderPath = Environment.GetEnvironmentVariable("TEMP");
                filePath = Path.Combine(tempFolderPath, filename);

                DateTime routeDate = route.StartTime.Value.Date;

                var exportResults = GrfExporter.ExportToGRF(filePath, route, _app.Project, _app.Geocoder,
                    _app.Solver, selectedPropertyNames, _grfExporterConfig.RouteGrfCompression);

                // Fill E-Mail message
                string subject = string.Format(Properties.Resources.GRFLetterSubjectFormat,
                    route.Vehicle.Name, routeDate.ToString("yyyyMMdd"));
                string[] attachments = new string[1];
                attachments[0] = filePath;

                // Try to send E-Mail message
                if (_mailer == null)
                {
                    if (_mailerException != null)
                        throw _mailerException;
                    else
                    {
                        try
                        {
                            _mailer = new Mailer(_grfExporterConfig);
                        }
                        catch (Exception ex)
                        {
                            _mailerException = ex;
                            throw;
                        }
                    }
                }

                _mailer.Send(mobileDevice.EmailAddress, mobileDevice.EmailAddress, subject, "", attachments, filePath);

                return exportResults;
            }
            finally
            {
                _DeleteFile(filePath);
            }
        }
        /// <summary>
        /// Send GRF to device via active sync
        /// </summary>
        /// <param name="route">Route to export</param>
        /// <param name="filename">GRF filename</param>
        /// <param name="selectedPropertyNames">Names of selected properties</param>
        /// <param name="mobileDevice">Mobile device</param>
        private GrfExportResult _SendToActiveSync(Route route, string filename, IList<string> selectedPropertyNames, MobileDevice mobileDevice)
        {
            if (string.IsNullOrEmpty(mobileDevice.ActiveSyncProfileName))
                throw new SettingsException(Properties.Resources.ActiveSyncProfileEmpty);

            // Try to find device name in registry
            string deviceKeyPath = _FindDeviceKeyPath(mobileDevice);
            bool deviceFound = deviceKeyPath.Length > 0;

            string syncPath = string.Empty;
            if (deviceFound)
            {
                string syncDeviceKeyPath = Path.Combine(deviceKeyPath, MobileDevice.PARTNER_SYNC_PATH);

                RegistryKey partnerRegKey = Registry.CurrentUser.OpenSubKey(syncDeviceKeyPath);

                // Get synchronization path for device
                syncPath = (string)partnerRegKey.GetValue(MobileDevice.BRIEFCASEPATH);
                if (string.IsNullOrEmpty(syncPath))
                {
                    string message = string.Format(Properties.Resources.SyncPathAbsent,
                        mobileDevice.ActiveSyncProfileName);
                    throw new SettingsException(message);
                }

                if (syncPath[0] == '\\')
                    syncPath = syncPath.Remove(0, 1);
            }
            else
            {
                string message = string.Format(Properties.Resources.DeviceNotFound,
                    mobileDevice.ActiveSyncProfileName);
                throw new SettingsException(message);
            }

            string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string deviceSyncFullPath = Path.Combine(documentsFolder, syncPath);
            string filePath = Path.Combine(deviceSyncFullPath, filename);

            return GrfExporter.ExportToGRF(filePath, route, _app.Project, _app.Geocoder,
                    _app.Solver, selectedPropertyNames, false);
        }
        /// <summary>
        /// Find key path in registry for mobile device
        /// </summary>
        /// <param name="mobileDevice">Mobile device</param>
        /// <returns>Registry key</returns>
        private string _FindDeviceKeyPath(MobileDevice mobileDevice)
        {
            string deviceKeyPath = string.Empty;

            RegistryKey regKey = Registry.CurrentUser.OpenSubKey(MobileDevice.PARTNERS_KEY_PATH);
            if (regKey != null)
            {
                string[] partnerNames = regKey.GetSubKeyNames();
                foreach (string partnerName in partnerNames)
                {
                    string partnerKey = Path.Combine(MobileDevice.PARTNERS_KEY_PATH, partnerName);
                    RegistryKey partnerRegKey = Registry.CurrentUser.OpenSubKey(partnerKey);
                    if (partnerRegKey != null)
                    {
                        string partnerDisplayName = (string)partnerRegKey.GetValue(MobileDevice.PARTNERS_DISPLAYNAME);
                        if (partnerDisplayName.Equals(mobileDevice.ActiveSyncProfileName, StringComparison.OrdinalIgnoreCase))
                        {
                            deviceKeyPath = partnerKey;
                            partnerRegKey.Close();
                            break;
                        }
                    }

                    partnerRegKey.Close();
                }

                regKey.Close();
            }

            return deviceKeyPath;
        }