mirror of
https://github.com/status-im/status-desktop.git
synced 2025-01-22 04:21:44 +00:00
refactor(@desktop/general): globalUtils
instance added (changed old utilsModel
)
This commit is contained in:
parent
f41ed8a522
commit
6e1438d241
@ -42,99 +42,10 @@ QtObject:
|
||||
result.statusFoundation = statusFoundation
|
||||
result.setup
|
||||
|
||||
proc getOs*(self: UtilsView): string {.slot.} =
|
||||
if defined(windows):
|
||||
return "windows"
|
||||
elif (defined(macosx)):
|
||||
return "mac"
|
||||
elif (defined(linux)):
|
||||
return "linux"
|
||||
return "unknown"
|
||||
|
||||
proc joinPath*(self: UtilsView, start: string, ending: string): string {.slot.} =
|
||||
result = os.joinPath(start, ending)
|
||||
|
||||
# proc join3Paths*(self: UtilsView, start: string, middle: string, ending: string): string {.slot.} =
|
||||
# result = os.joinPath(start, middle, ending)
|
||||
|
||||
# proc getSNTAddress*(self: UtilsView): string {.slot.} =
|
||||
# result = status_tokens.getSNTAddress()
|
||||
|
||||
proc getSNTBalance*(self: UtilsView): string {.slot.} =
|
||||
let currAcct = self.status.wallet.getWalletAccounts()[0]
|
||||
result = status_tokens.getSNTBalance($currAcct.address)
|
||||
|
||||
proc eth2Wei*(self: UtilsView, eth: string, decimals: int): string {.slot.} =
|
||||
let uintValue = status_utils.eth2Wei(parseFloat(eth), decimals)
|
||||
return uintValue.toString()
|
||||
|
||||
proc eth2Hex*(self: UtilsView, eth: float): string {.slot.} =
|
||||
return "0x" & status_utils.eth2Wei(eth, 18).toHex()
|
||||
|
||||
proc gwei2Hex*(self: UtilsView, gwei: float): string {.slot.} =
|
||||
return "0x" & status_utils.gwei2wei(gwei).toHex()
|
||||
|
||||
proc getStickerMarketAddress(self: UtilsView): string {.slot.} =
|
||||
$self.status.stickers.getStickerMarketAddress
|
||||
|
||||
QtProperty[string] stickerMarketAddress:
|
||||
read = getStickerMarketAddress
|
||||
|
||||
proc getEnsRegisterAddress(self: UtilsView): QVariant {.slot.} =
|
||||
newQVariant($statusRegistrarAddress())
|
||||
|
||||
QtProperty[QVariant] ensRegisterAddress:
|
||||
read = getEnsRegisterAddress
|
||||
|
||||
proc stripTrailingZeroes(value: string): string =
|
||||
var str = value.strip(leading = false, chars = {'0'})
|
||||
if str[str.len - 1] == '.':
|
||||
add(str, "0")
|
||||
return str
|
||||
|
||||
proc hex2Ascii*(self: UtilsView, value: string): string {.slot.} =
|
||||
result = string.fromBytes(hexToSeqByte(value))
|
||||
|
||||
proc ascii2Hex*(self: UtilsView, value: string): string {.slot.} =
|
||||
result = "0x" & toHex(value)
|
||||
|
||||
proc hex2Eth*(self: UtilsView, value: string): string {.slot.} =
|
||||
return stripTrailingZeroes(status_utils.wei2Eth(stint.fromHex(StUint[256], value)))
|
||||
|
||||
proc hex2Dec*(self: UtilsView, value: string): string {.slot.} =
|
||||
# somehow this value crashes the app
|
||||
if value == "0x0":
|
||||
return "0"
|
||||
return $stint.fromHex(StUint[256], value)
|
||||
|
||||
proc urlFromUserInput*(self: UtilsView, input: string): string {.slot.} =
|
||||
result = url_fromUserInput(input)
|
||||
|
||||
proc wei2Eth*(self: UtilsView, wei: string, decimals: int): string {.slot.} =
|
||||
var weiValue = wei
|
||||
if(weiValue.startsWith("0x")):
|
||||
weiValue = fromHex(Stuint[256], weiValue).toString()
|
||||
return status_utils.wei2Eth(weiValue, decimals)
|
||||
|
||||
proc generateAlias*(self: UtilsView, pk: string): string {.slot.} =
|
||||
result = self.status.accounts.generateAlias(pk)
|
||||
|
||||
proc generateIdenticon*(self: UtilsView, pk: string): string {.slot.} =
|
||||
result = self.status.accounts.generateIdenticon(pk)
|
||||
|
||||
proc getNetworkName*(self: UtilsView): string {.slot.} =
|
||||
self.status.settings.getCurrentNetworkDetails().name
|
||||
|
||||
proc getFileSize*(self: UtilsView, filename: string): string {.slot.} =
|
||||
var f: File = nil
|
||||
if f.open(filename.formatImagePath):
|
||||
try:
|
||||
result = $(f.getFileSize())
|
||||
finally:
|
||||
close(f)
|
||||
else:
|
||||
raise newException(IOError, "cannot open: " & filename)
|
||||
|
||||
proc getCurrentVersion*(self: UtilsView): string {.slot.} =
|
||||
return DESKTOP_VERSION
|
||||
|
||||
@ -196,17 +107,4 @@ QtObject:
|
||||
|
||||
QtProperty[string] newVersion:
|
||||
read = getNewVersion
|
||||
notify = newVersionChanged
|
||||
|
||||
proc readTextFile*(self: UtilsView, filepath: string): string {.slot.} =
|
||||
try:
|
||||
return readFile(filepath)
|
||||
except:
|
||||
return ""
|
||||
|
||||
proc writeTextFile*(self: UtilsView, filepath: string, text: string): bool {.slot.} =
|
||||
try:
|
||||
writeFile(filepath, text)
|
||||
return true
|
||||
except:
|
||||
return false
|
||||
notify = newVersionChanged
|
@ -68,6 +68,7 @@ type
|
||||
localAccountSettingsVariant: QVariant
|
||||
localAccountSensitiveSettingsVariant: QVariant
|
||||
userProfileVariant: QVariant
|
||||
globalUtilsVariant: QVariant
|
||||
|
||||
# Services
|
||||
osNotificationService: os_notification_service.Service
|
||||
@ -130,6 +131,7 @@ proc newAppController*(statusFoundation: StatusFoundation): AppController =
|
||||
result.localAccountSettingsVariant = newQVariant(singletonInstance.localAccountSettings)
|
||||
result.localAccountSensitiveSettingsVariant = newQVariant(singletonInstance.localAccountSensitiveSettings)
|
||||
result.userProfileVariant = newQVariant(singletonInstance.userProfile)
|
||||
result.globalUtilsVariant = newQVariant(singletonInstance.utils)
|
||||
|
||||
# Services
|
||||
result.settingsService = settings_service.newService()
|
||||
@ -224,6 +226,7 @@ proc delete*(self: AppController) =
|
||||
self.localAccountSettingsVariant.delete
|
||||
self.localAccountSensitiveSettingsVariant.delete
|
||||
self.userProfileVariant.delete
|
||||
self.globalUtilsVariant.delete
|
||||
|
||||
self.accountsService.delete
|
||||
self.chatService.delete
|
||||
@ -286,6 +289,7 @@ proc load(self: AppController) =
|
||||
let pubKey = self.settingsService.getPublicKey()
|
||||
singletonInstance.localAccountSensitiveSettings.setFileName(pubKey)
|
||||
singletonInstance.engine.setRootContextProperty("localAccountSensitiveSettings", self.localAccountSensitiveSettingsVariant)
|
||||
singletonInstance.engine.setRootContextProperty("globalUtils", self.globalUtilsVariant)
|
||||
|
||||
# other global instances
|
||||
self.buildAndRegisterLocalAccountSensitiveSettings()
|
||||
@ -335,4 +339,4 @@ proc buildAndRegisterUserProfile(self: AppController) =
|
||||
singletonInstance.userProfile.setLargeImage(large)
|
||||
singletonInstance.userProfile.setUserStatus(sendUserStatus)
|
||||
|
||||
singletonInstance.engine.setRootContextProperty("userProfile", self.userProfileVariant)
|
||||
singletonInstance.engine.setRootContextProperty("userProfile", self.userProfileVariant)
|
@ -4,11 +4,13 @@ import local_account_settings
|
||||
import local_account_sensitive_settings
|
||||
import local_app_settings
|
||||
import user_profile
|
||||
import utils
|
||||
|
||||
export local_account_settings
|
||||
export local_account_sensitive_settings
|
||||
export local_app_settings
|
||||
export user_profile
|
||||
export utils
|
||||
|
||||
type
|
||||
GlobalSingleton = object
|
||||
@ -52,6 +54,13 @@ proc userProfile*(self: GlobalSingleton): UserProfile =
|
||||
|
||||
return userProfile
|
||||
|
||||
proc utils*(self: GlobalSingleton): Utils =
|
||||
var utils {.global.}: Utils
|
||||
if (utils.isNil):
|
||||
utils = newUtils()
|
||||
|
||||
return utils
|
||||
|
||||
proc delete*(self: GlobalSingleton) =
|
||||
self.engine.delete()
|
||||
self.localAccountSettings.delete()
|
||||
|
90
src/app/global/utils.nim
Normal file
90
src/app/global/utils.nim
Normal file
@ -0,0 +1,90 @@
|
||||
import NimQml, strutils, uri
|
||||
import stew/byteutils
|
||||
import stint
|
||||
|
||||
# Services as instances shouldn't be used in this class, just some general/global procs
|
||||
import ../../app_service/service/eth/utils as procs_from_utils
|
||||
import ../../app_service/service/accounts/service as procs_from_accounts
|
||||
|
||||
|
||||
QtObject:
|
||||
type Utils* = ref object of QObject
|
||||
|
||||
proc setup(self: Utils) =
|
||||
self.QObject.setup
|
||||
|
||||
proc delete*(self: Utils) =
|
||||
self.QObject.delete
|
||||
|
||||
proc newUtils*(): Utils =
|
||||
new(result, delete)
|
||||
result.setup
|
||||
|
||||
proc formatImagePath*(self: Utils, imagePath: string): string =
|
||||
result = uri.decodeUrl(replace(imagePath, "file://", ""))
|
||||
if defined(windows):
|
||||
# Windows doesn't work with paths starting with a slash
|
||||
result.removePrefix('/')
|
||||
|
||||
proc urlFromUserInput*(self: Utils, input: string): string {.slot.} =
|
||||
result = url_fromUserInput(input)
|
||||
|
||||
proc eth2Wei*(self: Utils, eth: string, decimals: int): string {.slot.} =
|
||||
let uintValue = procs_from_utils.eth2Wei(parseFloat(eth), decimals)
|
||||
return uintValue.toString()
|
||||
|
||||
proc wei2Eth*(self: Utils, wei: string, decimals: int): string {.slot.} =
|
||||
var weiValue = wei
|
||||
if(weiValue.startsWith("0x")):
|
||||
weiValue = fromHex(Stuint[256], weiValue).toString()
|
||||
return procs_from_utils.wei2Eth(weiValue, decimals)
|
||||
|
||||
proc hex2Ascii*(self: Utils, value: string): string {.slot.} =
|
||||
result = string.fromBytes(hexToSeqByte(value))
|
||||
|
||||
proc ascii2Hex*(self: Utils, value: string): string {.slot.} =
|
||||
result = "0x" & toHex(value)
|
||||
|
||||
proc stripTrailingZeroes(value: string): string =
|
||||
var str = value.strip(leading = false, chars = {'0'})
|
||||
if str[str.len - 1] == '.':
|
||||
add(str, "0")
|
||||
return str
|
||||
|
||||
proc hex2Eth*(self: Utils, value: string): string {.slot.} =
|
||||
return stripTrailingZeroes(procs_from_utils.wei2Eth(stint.fromHex(StUint[256], value)))
|
||||
|
||||
proc hex2Dec*(self: Utils, value: string): string {.slot.} =
|
||||
# somehow this value crashes the app
|
||||
if value == "0x0":
|
||||
return "0"
|
||||
return $stint.fromHex(StUint[256], value)
|
||||
|
||||
proc generateAlias*(self: Utils, pk: string): string {.slot.} =
|
||||
return procs_from_accounts.generateAliasFromPk(pk)
|
||||
|
||||
proc generateIdenticon*(self: Utils, pk: string): string {.slot.} =
|
||||
return procs_from_accounts.generateIdenticonFromPk(pk)
|
||||
|
||||
proc getFileSize*(self: Utils, filename: string): string {.slot.} =
|
||||
var f: File = nil
|
||||
if f.open(self.formatImagePath(filename)):
|
||||
try:
|
||||
result = $(f.getFileSize())
|
||||
finally:
|
||||
close(f)
|
||||
else:
|
||||
raise newException(IOError, "cannot open: " & filename)
|
||||
|
||||
proc readTextFile*(self: Utils, filepath: string): string {.slot.} =
|
||||
try:
|
||||
return readFile(filepath)
|
||||
except:
|
||||
return ""
|
||||
|
||||
proc writeTextFile*(self: Utils, filepath: string, text: string): bool {.slot.} =
|
||||
try:
|
||||
writeFile(filepath, text)
|
||||
return true
|
||||
except:
|
||||
return false
|
@ -5,7 +5,7 @@ import ./model
|
||||
import ./io_interface
|
||||
import status/profile as status_profile
|
||||
import status/status
|
||||
import ../../../../utils/image_utils
|
||||
import ../../../../global/global_singleton
|
||||
|
||||
import status/types/[identity_image, profile]
|
||||
|
||||
@ -45,7 +45,7 @@ QtObject:
|
||||
self.modelChanged()
|
||||
|
||||
proc upload*(self: View, imageUrl: string, aX: int, aY: int, bX: int, bY: int): string {.slot.} =
|
||||
var image = image_utils.formatImagePath(imageUrl)
|
||||
var image = singletonInstance.utils.formatImagePath(imageUrl)
|
||||
# FIXME the function to get the file size is messed up
|
||||
# var size = image_getFileSize(image)
|
||||
# TODO find a way to i18n this (maybe send just a code and then QML sets the right string)
|
||||
|
@ -1,8 +0,0 @@
|
||||
import strutils
|
||||
import uri as uri
|
||||
|
||||
proc formatImagePath*(imagePath: string): string =
|
||||
result = uri.decodeUrl(replace(imagePath, "file://", ""))
|
||||
if defined(windows):
|
||||
# Windows doesn't work with paths starting with a slash
|
||||
result.removePrefix('/')
|
@ -1,4 +1,4 @@
|
||||
import Tables, json, sequtils, strutils, strformat, uuids
|
||||
import json, sequtils, strutils, uuids
|
||||
import json_serialization, chronicles
|
||||
|
||||
import service_interface
|
||||
@ -42,9 +42,12 @@ method getImportedAccount*(self: Service): GeneratedAccountDto =
|
||||
method isFirstTimeAccountLogin*(self: Service): bool =
|
||||
return self.isFirstTimeAccountLogin
|
||||
|
||||
method generateAlias*(self: Service, publicKey: string): string =
|
||||
proc generateAliasFromPk*(publicKey: string): string =
|
||||
return status_account.generateAlias(publicKey).result.getStr
|
||||
|
||||
proc generateIdenticonFromPk*(publicKey: string): string =
|
||||
return status_account.generateIdenticon(publicKey).result.getStr
|
||||
|
||||
method init*(self: Service) =
|
||||
try:
|
||||
let response = status_account.generateAddresses(PATHS)
|
||||
@ -53,10 +56,8 @@ method init*(self: Service) =
|
||||
proc(x: JsonNode): GeneratedAccountDto = toGeneratedAccountDto(x))
|
||||
|
||||
for account in self.generatedAccounts.mitems:
|
||||
account.alias = self.generateAlias(account.derivedAccounts.whisper.publicKey)
|
||||
|
||||
let responseIdenticon = status_account.generateIdenticon(account.derivedAccounts.whisper.publicKey)
|
||||
account.identicon = responseIdenticon.result.getStr
|
||||
account.alias = generateAliasFromPk(account.derivedAccounts.whisper.publicKey)
|
||||
account.identicon = generateIdenticonFromPk(account.derivedAccounts.whisper.publicKey)
|
||||
|
||||
except Exception as e:
|
||||
error "error: ", methodName="init", errName = e.name, errDesription = e.msg
|
||||
@ -276,10 +277,8 @@ method importMnemonic*(self: Service, mnemonic: string): bool =
|
||||
let responseDerived = status_account.deriveAccounts(self.importedAccount.id, PATHS)
|
||||
self.importedAccount.derivedAccounts = toDerivedAccounts(responseDerived.result)
|
||||
|
||||
self.importedAccount.alias= self.generateAlias(self.importedAccount.derivedAccounts.whisper.publicKey)
|
||||
|
||||
let responseIdenticon = status_account.generateIdenticon(self.importedAccount.derivedAccounts.whisper.publicKey)
|
||||
self.importedAccount.identicon = responseIdenticon.result.getStr
|
||||
self.importedAccount.alias= generateAliasFromPk(self.importedAccount.derivedAccounts.whisper.publicKey)
|
||||
self.importedAccount.identicon = generateIdenticonFromPk(self.importedAccount.derivedAccounts.whisper.publicKey)
|
||||
|
||||
return self.importedAccount.isValid()
|
||||
|
||||
|
@ -6,7 +6,7 @@ import ../activity_center/dto/notification as notification_dto
|
||||
import ../contacts/service as contact_service
|
||||
import status/statusgo_backend_new/chat as status_chat
|
||||
import status/statusgo_backend_new/chatCommands as status_chat_commands
|
||||
import ../../../app/utils/image_utils
|
||||
import ../../../app/global/global_singleton
|
||||
import ../../../constants
|
||||
|
||||
from ../../common/account_constants import ZERO_ADDRESS
|
||||
@ -241,7 +241,7 @@ QtObject:
|
||||
var images = Json.decode(imagePathsJson, seq[string])
|
||||
|
||||
for imagePath in images.mitems:
|
||||
var image = image_utils.formatImagePath(imagePath)
|
||||
var image = singletonInstance.utils.formatImagePath(imagePath)
|
||||
imagePath = image_resizer(image, 2000, TMPDIR)
|
||||
|
||||
discard status_chat.sendImages(chatId, images)
|
||||
|
@ -16,23 +16,19 @@ QtObject {
|
||||
property bool currentTabConnected: false
|
||||
|
||||
function getUrlFromUserInput(input) {
|
||||
// Not Refactored Yet
|
||||
// return utilsModel.urlFromUserInput(input)
|
||||
return globalUtils.urlFromUserInput(input)
|
||||
}
|
||||
|
||||
function getAscii2Hex(input) {
|
||||
// Not Refactored Yet
|
||||
// return utilsModel.ascii2Hex(input)
|
||||
return globalUtils.ascii2Hex(input)
|
||||
}
|
||||
|
||||
function getHex2Ascii(input) {
|
||||
// Not Refactored Yet
|
||||
// return utilsModel.hex2Ascii(input)
|
||||
return globalUtils.hex2Ascii(input)
|
||||
}
|
||||
|
||||
function getWei2Eth(wei,decimals) {
|
||||
// Not Refactored Yet
|
||||
// return utilsModel.wei2Eth(wei,decimals)
|
||||
return globalUtils.wei2Eth(wei,decimals)
|
||||
}
|
||||
|
||||
function generateIdenticon(pk) {
|
||||
|
@ -78,8 +78,8 @@ Item {
|
||||
}
|
||||
|
||||
function requestAddressForTransaction(address, amount, tokenAddress, tokenDecimals = 18) {
|
||||
amount = globalUtils.eth2Wei(amount.toString(), tokenDecimals)
|
||||
// Not Refactored Yet
|
||||
// amount = root.rootStore.utilsModelInst.eth2Wei(amount.toString(), tokenDecimals)
|
||||
// root.rootStore.chatsModelInst.transactions.requestAddress(activeChatId,
|
||||
// address,
|
||||
// amount,
|
||||
@ -87,8 +87,8 @@ Item {
|
||||
txModalLoader.close()
|
||||
}
|
||||
function requestTransaction(address, amount, tokenAddress, tokenDecimals = 18) {
|
||||
amount = globalUtils.eth2Wei(amount.toString(), tokenDecimals)
|
||||
// Not Refactored Yet
|
||||
// amount = root.rootStore.utilsModelInst.eth2Wei(amount.toString(), tokenDecimals)
|
||||
// root.rootStore.chatsModelInst.transactions.request(activeChatId,
|
||||
// address,
|
||||
// amount,
|
||||
|
@ -304,7 +304,7 @@ QtObject {
|
||||
}
|
||||
|
||||
function getSntBalance() {
|
||||
// Not Refactored Yet
|
||||
// Not Refactored Yet - This should be fetched from corresponding module, not from the global Utils
|
||||
// return utilsModelInst.getSNTBalance()
|
||||
}
|
||||
|
||||
@ -397,13 +397,11 @@ QtObject {
|
||||
}
|
||||
|
||||
function readTextFile(path) {
|
||||
// Not Refactored Yet
|
||||
// return utilsModelInst.readTextFile(path)
|
||||
return globalUtils.readTextFile(path)
|
||||
}
|
||||
|
||||
function writeTextFile(path, value) {
|
||||
// Not Refactored Yet
|
||||
// utilsModelInst.writeTextFile(path, value)
|
||||
globalUtils.writeTextFile(path, value)
|
||||
}
|
||||
|
||||
function setMessagesFromContactsOnly(checked) {
|
||||
|
@ -144,15 +144,11 @@ QtObject {
|
||||
}
|
||||
|
||||
function hex2Dec(value) {
|
||||
// TODO: Move to transaction root module and not wallet
|
||||
// Not Refactored Yet
|
||||
// return utilsModel.hex2Dec(value)
|
||||
return globalUtils.hex2Dec(value)
|
||||
}
|
||||
|
||||
function hex2Eth(value) {
|
||||
// TODO: Move to transaction module
|
||||
// Not Refactored Yet
|
||||
// return utilsModel.hex2Eth(value)
|
||||
return globalUtils.hex2Eth(value)
|
||||
}
|
||||
|
||||
function checkRecentHistory() {
|
||||
|
@ -11,15 +11,14 @@ StatusChatImageValidator {
|
||||
readonly property int maxImgSizeBytes: Constants.maxUploadFilesizeMB * 1048576 /* 1 MB in bytes */
|
||||
|
||||
onImagesChanged: {
|
||||
// Not Refactored Yet
|
||||
// let isValid = true
|
||||
// root.validImages = images.filter(img => {
|
||||
// let size = parseInt(utilsModel.getFileSize(img))
|
||||
// const isSmallEnough = size <= maxImgSizeBytes
|
||||
// isValid = isValid && isSmallEnough
|
||||
// return isSmallEnough
|
||||
// })
|
||||
// root.isValid = isValid
|
||||
let isValid = true
|
||||
root.validImages = images.filter(img => {
|
||||
let size = parseInt(globalUtils.getFileSize(img))
|
||||
const isSmallEnough = size <= maxImgSizeBytes
|
||||
isValid = isValid && isSmallEnough
|
||||
return isSmallEnough
|
||||
})
|
||||
root.isValid = isValid
|
||||
}
|
||||
//% "Max image size is %1 MB"
|
||||
errorMessage: qsTrId("max-image-size-is--1-mb").arg(Constants.maxUploadFilesizeMB)
|
||||
|
Loading…
x
Reference in New Issue
Block a user