コード例 #1
0
 private void UpdateView(OmemoAccountInformationModel omemoInfo)
 {
     if (omemoInfo is not null)
     {
         MODEL.DeviceLabel = string.IsNullOrEmpty(omemoInfo.deviceLabel) ? (omemoInfo.deviceId == 0 ? "-" : omemoInfo.deviceId.ToString()) : omemoInfo.deviceLabel;
     }
 }
コード例 #2
0
        public void SaveDeviceLabel(string deviceLabel, Client client)
        {
            OmemoAccountInformationModel omemoInfo = client.dbAccount.omemoInfo;

            deviceLabel = deviceLabel.Trim();

            string newLabel;

            if (string.IsNullOrEmpty(deviceLabel) || string.Equals(deviceLabel, omemoInfo.deviceId.ToString()))
            {
                newLabel = null;
            }
            else
            {
                newLabel = deviceLabel;
            }

            // Prevent unnecessary updates in case for example the control loaded:
            if (string.Equals(newLabel, omemoInfo.deviceLabel))
            {
                return;
            }

            MODEL.Saving = true;
            Task.Run(async() =>
            {
                try
                {
                    if (client.xmppClient.isConnected())
                    {
                        MessageResponseHelperResult <IQMessage> result = await client.xmppClient.OMEMO_COMMAND_HELPER.requestDeviceListAsync(client.dbAccount.bareJid);
                        if (result.STATE == MessageResponseHelperResultState.SUCCESS)
                        {
                            if (result.RESULT is OmemoDeviceListResultMessage deviceListResultMessage)
                            {
                                OmemoXmlDevice device = deviceListResultMessage.DEVICES.DEVICES.Where(d => d.ID == omemoInfo.deviceId).FirstOrDefault();
                                if (device is null)
                                {
                                    device = new OmemoXmlDevice(omemoInfo.deviceId, deviceLabel);
                                    deviceListResultMessage.DEVICES.DEVICES.Add(device);
                                }
                                else if (string.Equals(device.label, deviceLabel))
                                {
                                    MODEL.ErrorSaving = false;
                                    MODEL.Saving      = false;
                                    Logger.Info("No need to update devices. Label already the same.");
                                    return;
                                }
                                else
                                {
                                    device.label = newLabel;
                                    result       = await client.xmppClient.OMEMO_COMMAND_HELPER.setDeviceListAsync(deviceListResultMessage.DEVICES);
                                    if (result.STATE != MessageResponseHelperResultState.SUCCESS)
                                    {
                                        Logger.Error("Failed to set device list (" + result.STATE + ").");
                                    }
                                    else if (result.RESULT is IQErrorMessage errorMessage)
                                    {
                                        Logger.Error("Failed to set device list (" + errorMessage.ERROR_OBJ.ToString() + ").");
                                    }
                                    else
                                    {
                                        MODEL.ErrorSaving     = false;
                                        omemoInfo.deviceLabel = newLabel;
                                        omemoInfo.Update();
                                        MODEL.Saving = false;
                                        Logger.Info($"Device label for device {omemoInfo.deviceId} successfully updated to: '{newLabel}'.");
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                Logger.Error("Failed to request device list (" + result.RESULT.ToString() + ").");
                            }
                        }
                        else
                        {
                            Logger.Error("Failed to request device list (" + result.STATE.ToString() + ").");
                        }
                    }
                    else
                    {
                        Logger.Error("Failed to update device label. Client not connected");
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to update device label.", e);
                }
                MODEL.Saving      = false;
                MODEL.ErrorSaving = true;
            });
        }