Compare commits

..

3 commits

Author SHA1 Message Date
edad2578a9
Git: Ignore result symlink created by nix 2024-08-28 15:40:21 +02:00
da4cf59e25
Nix: Add support for building using nix
This patch adds a default nix which invokes a `callPackage`
to build sources from the current working directory.

To invoke the build run `nix-build`.
2024-08-28 15:40:20 +02:00
0d2f07fc0a
Dependencies: Update lock file 2024-08-28 15:40:19 +02:00
23 changed files with 556 additions and 1654 deletions

554
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,3 +0,0 @@
app_id = "de.j4ne.Paket"
icons = ["plus", "minus"]

View file

@ -19,7 +19,6 @@ secrecy = { version = "0.8.0", features = ["serde"] }
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
serde_repr = { version = "0.1.18", optional = true }
serde_ignored = "0.1"
url = "2.5.0"
base64 = "0.22"
@ -36,12 +35,9 @@ thiserror = "1.0.56"
[features]
default = [
"advices",
"locker_all",
"unstable",
"locker_all"
]
unstable = []
advices = [
#"dep:sha2",
"dep:uuid",
@ -77,4 +73,4 @@ locker_register_base = [
locker_register_regtoken = [
"locker_register_base"
]
]

View file

@ -10,13 +10,6 @@ This is an unofficial client to various DHL APIs, more specific the ones that th
- app-driven parcel lockers (App-gesteuerte Packstation)
## Deprecation notice
It's recommended that consumers of this crate are always tracking the latest version. If the upstream API changes too much or a better API here is created, there are two ways to mark a function or struct deprecated:
- Soft deprecation/Crate API changes: The normal deprecation notice via `#[deprecated]` (see the [RFC#1270](https://github.com/rust-lang/rfcs/blob/master/text/1270-deprecation.md) or the [rust reference docs](https://doc.rust-lang.org/reference/attributes/diagnostics.html)).
- (Fatal) Upstream API Changes: The soft deprecation marked with `deny(deprecated)`, you can try to override this, but functions with a `LibraryResult` will always return `LibraryError::Deprecated`. (NOTE: `LibrarayError::APIChange` is reserved for the case where the API-change is unknown. Please upgrade to a or wait for a newer crate version.)
## Examples
In the examples error-handling is ignored for simplicity. You dont want to do that.

View file

@ -1,10 +1,10 @@
pub use super::Advice;
use super::AdvicesResponse;
use crate::constants::webview_user_agent;
use crate::LibraryResult;
use reqwest::header::HeaderMap;
use serde::Serialize;
use crate::constants::webview_user_agent;
use crate::LibraryResult;
pub struct AdviceClient {
client: reqwest::Client,
@ -27,10 +27,7 @@ impl AdviceClient {
pub async fn access_token<'t>(&self, advices: &AdvicesResponse) -> LibraryResult<UatToken> {
mini_assert_inval!(advices.has_any_advices());
mini_assert_api_eq!(
advices.access_token_url.as_ref().unwrap().as_str(),
endpoint_access_tokens()
);
mini_assert_api_eq!(advices.access_token_url.as_ref().unwrap().as_str(), endpoint_access_tokens());
let req = self
.client
@ -49,13 +46,10 @@ impl AdviceClient {
let res = res.unwrap();
for cookie in res.cookies() {
println!("UAT: cookie: {:?}={:?}", cookie.name(), cookie.value());
if cookie.name() == "AccessToken" {
if cookie.name() == "UAT" {
return Ok(UatToken(cookie.value().to_string()));
}
}
println!("UAT: headers: {:?}", res.headers());
println!("UAT: text: {:?}", res.text().await);
// FIXME: Parse errors here better (checking if we're unauthorized,...)
panic!("NO UAT Token in access_token");
}
@ -92,10 +86,9 @@ impl AdviceClient {
)
};
let req = self
.client
let req = self.client
.get(&advice.image_url)
.header("Cookie", format!("AccessToken={}", uat.0))
.header("Cookie", format!("UAT={}", uat.0))
.build()
.unwrap();
let res = self.client.execute(req).await;
@ -157,5 +150,5 @@ fn headers() -> HeaderMap {
}
pub fn endpoint_access_tokens() -> &'static str {
"https://briefankuendigung.enplify.dhl.de/pdapp-web/access-tokens"
"https://briefankuendigung.dhl.de/pdapp-web/access-tokens"
}

View file

@ -29,13 +29,18 @@ newtype! {
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AdvicesResponse {
// access_token_url, basic_auth, grant_token is null if no advices are available
#[serde(rename = "accessTokenUrl")]
pub(super) access_token_url: Option<AdviceAccessTokenUrl>,
#[serde(rename = "basicAuth")]
pub(super) basic_auth: Option<String>,
#[serde(rename = "currentAdvice")]
current_advice: Option<AdvicesList>,
#[serde(rename = "grantToken")]
pub(super) grant_token: Option<String>,
#[serde(rename = "oldAdvices")]
old_advices: Vec<AdvicesList>,
}
@ -68,11 +73,33 @@ fn endpoint_advices() -> url::Url {
impl crate::www::WebClient {
// FIXME: more error parsing
pub async fn advices(&self, dhli: &crate::login::DHLIdToken) -> crate::LibraryResult<AdvicesResponse> {
let res = request!(self.web_client, endpoint_advices,
header("Cookie", CookieHeaderValueBuilder::new().add_dhli(dhli).add_dhlcs(dhli).build_string())
);
let cookie_headervalue = CookieHeaderValueBuilder::new()
.add_dhli(dhli)
.add_dhlcs(dhli)
.build_string();
let res = parse_json_response!(res, AdvicesResponse);
let req = self.web_client
.get(endpoint_advices().clone())
.header("Cookie", cookie_headervalue)
.build()
.unwrap();
let res = self.web_client.execute(req).await;
if let Err(err) = res {
return Err(err.into())
}
let res = res.unwrap();
let res_text = res.text().await.unwrap();
let res = serde_json::from_str::<AdvicesResponse>(&res_text);
let res = match res {
Ok(res) => res,
Err(err) => {
return Err(err.into())
}
};
if res.access_token_url.is_some() {
if res.access_token_url.as_ref().unwrap().as_str() != crate::advices::endpoint_access_tokens() {

View file

@ -1,6 +1,3 @@
#[macro_use]
mod utils;
mod www;
pub use www::WebClient;
@ -11,6 +8,8 @@ pub mod stammdaten;
pub use stammdaten::StammdatenClient;
mod common;
#[macro_use]
mod utils;
pub mod constants;
#[cfg(feature = "locker_base")]
@ -21,9 +20,6 @@ pub mod advices;
#[cfg(feature = "advices")]
pub use advices::AdviceClient;
#[cfg(feature = "unstable")]
pub mod tracking;
/*#[cfg(test)]
pub(crate) mod private;*/
@ -41,8 +37,6 @@ pub enum LibraryError {
DecodeError(String),
#[error("upstream api was changed. not continuing")]
APIChange,
#[error("upstream api was changed. this method is deprecated")]
Deprecated,
}
pub type LibraryResult<T> = Result<T, LibraryError>;
@ -69,4 +63,4 @@ impl From<serde_json::Error> for LibraryError {
fn from(value: serde_json::Error) -> Self {
Self::DecodeError(value.to_string())
}
}
}

View file

@ -27,7 +27,8 @@ fn headers() -> HeaderMap {
/* ("accept", "application/json") */
("app-version", app_version()),
("device-os", "Android"),
("device-key", ""), /* is the android id... */
("device-key", "") /* is the android id... */
];
let mut map = HeaderMap::new();

View file

@ -1,6 +1,5 @@
use num_enum::TryFromPrimitive;
use uuid::Uuid;
use super::utils::{PrimitiveBuilder, PrimitiveReader};
use crate::{LibraryError, LibraryResult};
@ -82,6 +81,104 @@ pub struct Command {
metadata: Vec<u8>
}
struct PrimitiveBuilder {
bin: Vec<u8>,
}
impl PrimitiveBuilder {
fn new() -> Self {
PrimitiveBuilder {
bin: Vec::new()
}
}
fn write_u8(self, number: u8) -> Self {
self.write_array(&[number])
}
fn write_u16(self, number: u16) -> Self {
self.write_array(&number.to_be_bytes())
}
fn write_u32(self, number: u32) -> Self {
self.write_array(&number.to_be_bytes())
}
fn write_array(mut self, new: &[u8]) -> Self {
for b in new {
self.bin.push(*b);
}
self
}
fn write_array_with_len(self, bin: &[u8]) -> Self {
self
.write_u32(bin.len() as u32)
.write_array(bin)
}
fn finish(self) -> Vec<u8> {
self.bin
}
}
struct PrimitiveReader<'a> {
offset: usize,
vec: &'a [u8],
}
// Yes, I know Cursor exists and eio exists. from_be_bytes should be a stdlib trait tho.
impl<'a> PrimitiveReader<'a> {
fn read_u8(&mut self) -> u8 {
let number = self.vec[self.offset];
self.offset += 1;
number
}
fn read_u16(&mut self) -> u16 {
let arr: [u8; 2] = self.read_arr_const();
u16::from_be_bytes(arr)
}
fn read_u32(&mut self) -> u32 {
let arr: [u8; 4] = self.read_arr_const();
u32::from_be_bytes(arr)
}
fn read_u64(&mut self) -> u64 {
let arr: [u8; 8] = self.read_arr_const();
u64::from_be_bytes(arr)
}
fn read_arr_const<const N: usize>(&mut self) -> [u8; N] {
let mut arr = [0u8; N];
for n in 0..N {
arr[n] = self.read_u8();
}
return arr;
}
fn read_arr(&mut self, n: usize) -> Vec<u8> {
let mut arr: Vec<u8> = vec![];
for _ in 0..n {
arr.push(self.read_u8());
}
arr
}
fn read_arr_from_len(&mut self) -> Vec<u8> {
let size = self.read_u32() as usize;
self.read_arr(size)
}
fn left_to_process(&self) -> usize {
self.vec.len() - self.offset
}
}
impl Command {
fn checksum(bin: &[u8]) -> u16 {
// CRC16 of some kind...

View file

@ -6,10 +6,9 @@ use ed25519_dalek::Signer;
pub struct CustomerKeySeed {
pub postnumber: String,
pub seed: Seed,
pub uuid: Uuid,
pub device_id: Option<String>,
postnumber: String,
seed: Seed,
uuid: Uuid,
}
pub struct Seed {
@ -45,25 +44,17 @@ impl CustomerKeySeed {
postnumber,
seed: Seed::random(),
uuid: uuid::Uuid::new_v4(),
device_id: None,
}
}
pub fn from(postnumber: &String, seed: Vec<u8>, uuid: &Uuid, device_id: String) -> Self {
pub fn from(postnumber: &String, seed: Vec<u8>, uuid: &Uuid) -> Self {
CustomerKeySeed {
postnumber: postnumber.clone(),
seed: Seed::from_bytes(seed),
uuid: uuid.clone(),
device_id: Some(device_id),
uuid: uuid.clone()
}
}
pub fn set_device_id(&mut self, device_id: String) {
assert!(self.device_id.is_none());
self.device_id = Some(device_id);
}
pub(crate) fn sign(&self, message: &[u8]) -> String {
let signing_key = SigningKey::from_bytes(self.seed.as_bytes());

View file

@ -1,24 +1,13 @@
#[cfg(feature = "locker_ble")]
#[cfg(feature = "unstable")]
mod command;
#[cfg(feature = "locker_ble")]
#[cfg(feature = "unstable")]
mod types;
#[cfg(feature = "locker_ble")]
#[cfg(feature = "unstable")]
pub use types::*;
#[cfg(feature = "locker_ble")]
#[cfg(feature = "unstable")]
mod api;
#[cfg(feature = "locker_ble")]
#[cfg(feature = "unstable")]
pub use api::*;
pub mod crypto;
pub(crate) mod utils;
#[cfg(feature = "locker_register_base")]
mod register_base;
#[cfg(feature = "locker_register_regtoken")]

View file

@ -1,107 +1,23 @@
use btleplug::platform::PeripheralId;
use super::utils::PrimitiveReader;
use crate::{LibraryError, LibraryResult};
use crate::LibraryError;
// 601e7028-0565-
pub static LOCKER_SERVICE_UUID_PREFIX: (u32, u16) = (0x601e7028, 0x0565);
pub enum LockerVendor {
Keba,
Snbc,
Unknown,
}
impl From<u8> for LockerVendor {
fn from(value: u8) -> Self {
if value == 1 {
Self::Keba
} else if value == 2 {
Self::Snbc
} else {
Self::Unknown
}
}
}
#[derive(Debug)]
pub struct LockerVersion {
pub tlv_version: u8,
pub vendor: u8,
pub major: u8,
pub minor: u8,
}
#[derive(Debug)]
pub struct LockerDevice {
pub id: PeripheralId,
pub service_uuid: LockerServiceUUID,
pub version: LockerVersion,
}
impl LockerDevice {
pub(crate) fn new(
id: PeripheralId,
service_uuid: LockerServiceUUID,
service_data: &Vec<u8>,
) -> LibraryResult<Self> {
mini_assert_inval!(service_data.len() == 14);
let mut reader = PrimitiveReader {
offset: 0,
vec: &service_data,
};
let version = LockerVersion {
tlv_version: reader.read_u8(),
vendor: reader.read_u8(),
major: reader.read_u8(),
minor: reader.read_u8(),
};
let part1: u16 = reader.read_u16();
let part2: u16 = reader.read_u16();
let part_last = reader.read_u32();
assert_eq!(reader.left_to_process(), 0);
println!(
"decoded: {:0>8x}-{:0>4x}-{:0>4x}-{:0>4x}-{:0>8x}",
LOCKER_SERVICE_UUID_PREFIX.0, LOCKER_SERVICE_UUID_PREFIX.1, part1, part2, part_last
);
println!("expected: {:?}", service_uuid);
Ok(LockerDevice {
id,
service_uuid,
version,
})
}
}
#[derive(Debug)]
pub struct LockerServiceUUID {
service_uuid: uuid::Uuid,
}
impl LockerServiceUUID {
pub fn get(&self) -> &uuid::Uuid {
&self.service_uuid
}
}
impl TryFrom<uuid::Uuid> for LockerServiceUUID {
type Error = crate::LibraryError;
fn try_from(value: uuid::Uuid) -> LibraryResult<Self> {
fn try_from(value: uuid::Uuid) -> Result<Self, Self::Error> {
let fields = value.as_fields();
if fields.0 != LOCKER_SERVICE_UUID_PREFIX.0 || fields.1 != LOCKER_SERVICE_UUID_PREFIX.1 {
return Err(LibraryError::InvalidArgument(
"TryFrom<Uuid> for LockerServiceUUID: prefix mismatch (expected 601e7028-0565-)"
.to_string(),
));
return Err(LibraryError::InvalidArgument("TryFrom<Uuid> for LockerServiceUUID: prefix mismatch (expected 601e7028-0565-)".to_string()))
}
Ok(LockerServiceUUID {
service_uuid: value,
service_uuid: value
})
}
}
@ -110,4 +26,4 @@ impl Into<uuid::Uuid> for LockerServiceUUID {
fn into(self) -> uuid::Uuid {
self.service_uuid
}
}
}

View file

@ -1,93 +0,0 @@
pub(crate) struct PrimitiveBuilder {
pub bin: Vec<u8>,
}
impl PrimitiveBuilder {
pub(crate) fn new() -> Self {
PrimitiveBuilder { bin: Vec::new() }
}
pub(crate) fn write_u8(self, number: u8) -> Self {
self.write_array(&[number])
}
pub(crate) fn write_u16(self, number: u16) -> Self {
self.write_array(&number.to_be_bytes())
}
pub(crate) fn write_u32(self, number: u32) -> Self {
self.write_array(&number.to_be_bytes())
}
pub(crate) fn write_array(mut self, new: &[u8]) -> Self {
for b in new {
self.bin.push(*b);
}
self
}
pub(crate) fn write_array_with_len(self, bin: &[u8]) -> Self {
self.write_u32(bin.len() as u32).write_array(bin)
}
pub(crate) fn finish(self) -> Vec<u8> {
self.bin
}
}
pub(crate) struct PrimitiveReader<'a> {
pub offset: usize,
pub vec: &'a [u8],
}
// Yes, I know Cursor exists and eio exists. from_be_bytes should be a stdlib trait tho.
impl<'a> PrimitiveReader<'a> {
pub(crate) fn read_u8(&mut self) -> u8 {
let number = self.vec[self.offset];
self.offset += 1;
number
}
pub(crate) fn read_u16(&mut self) -> u16 {
let arr: [u8; 2] = self.read_arr_const();
u16::from_be_bytes(arr)
}
pub(crate) fn read_u32(&mut self) -> u32 {
let arr: [u8; 4] = self.read_arr_const();
u32::from_be_bytes(arr)
}
pub(crate) fn read_u64(&mut self) -> u64 {
let arr: [u8; 8] = self.read_arr_const();
u64::from_be_bytes(arr)
}
pub(crate) fn read_arr_const<const N: usize>(&mut self) -> [u8; N] {
let mut arr = [0u8; N];
for n in 0..N {
arr[n] = self.read_u8();
}
return arr;
}
pub(crate) fn read_arr(&mut self, n: usize) -> Vec<u8> {
let mut arr: Vec<u8> = vec![];
for _ in 0..n {
arr.push(self.read_u8());
}
arr
}
pub(crate) fn read_arr_from_len(&mut self) -> Vec<u8> {
let size = self.read_u32() as usize;
self.read_arr(size)
}
pub(crate) fn left_to_process(&self) -> usize {
self.vec.len() - self.offset
}
}

View file

@ -4,12 +4,13 @@ mod openid_response;
pub mod openid_token;
mod utils;
pub use self::dhl_claims::{DHLCs, DHLIdToken};
pub use self::dhl_claims::{DHLIdToken, DHLCs};
pub use self::openid_response::{RefreshToken, TokenResponse};
pub use self::utils::{create_nonce, CodeVerfier};
pub use self::utils::{CodeVerfier, create_nonce};
use super::common::APIResult;
use crate::LibraryResult;
use crate::{LibraryError, LibraryResult};
pub struct OpenIdClient {
client: reqwest::Client,
@ -30,12 +31,27 @@ impl OpenIdClient {
&self,
refresh_token: &RefreshToken,
) -> LibraryResult<TokenResponse> {
let res = request_post!(self.client,
(constants::token::endpoint()),
let req = self
.client
.post(constants::token::endpoint())
.form(constants::token::refresh_token_form(refresh_token.as_str()).as_slice())
);
.build()
.unwrap();
Ok(parse_json_response_from_apiresult!(res, TokenResponse))
let res = self.client.execute(req).await;
match res {
Ok(res) => {
let res = res.text().await.unwrap();
let res = serde_json::from_str::<APIResult<TokenResponse>>(&res);
match res {
Ok(res) => return res.into(),
Err(err) => Err(LibraryError::from(err)),
}
}
Err(err) => Err(LibraryError::from(err)),
}
}
pub async fn token_authorization(
@ -62,9 +78,23 @@ impl OpenIdClient {
let headermap = req.headers_mut();
headermap.append("Content-Length", len.into());
}
let res = self.client.execute(req).await;
let res = parse_response_internal!(res);
Ok(parse_json_response_from_apiresult!(res, TokenResponse))
let res = self.client.execute(req).await;
println!("auth_code: {:?}", res);
match res {
Ok(res) => {
let res = res.text().await.unwrap();
println!("auth_code reply: {:?}", res);
let res = serde_json::from_str::<APIResult<TokenResponse>>(&res);
match res {
Ok(res) => res.into(),
Err(err) => Err(LibraryError::from(err)),
}
}
Err(err) => Err(LibraryError::from(err)),
}
}
}

View file

@ -1,446 +0,0 @@
use serde::{Deserialize, Serialize};
use crate::{login::DHLIdToken, LibraryResult};
#[derive(Default)]
pub struct TrackingParams {
pub language: Option<String>,
}
#[derive(Serialize)]
pub struct ShipmentRequest {
international: bool,
piececode: String,
zip: String,
}
impl crate::WebClient {
pub async fn tracking_search(
&self,
params: TrackingParams,
ids: Vec<String>,
dhli: Option<&DHLIdToken>,
) -> LibraryResult<Vec<Shipment>> {
let api_key = crate::www::api_key_header();
let res = if let Some(dhli) = dhli {
let cookie_value = crate::utils::CookieHeaderValueBuilder::new()
.add_dhli(&dhli)
.add_dhlcs(&dhli)
.build_string();
request!(
self.web_client,
endpoint_data_search,
query(&query_parameters_data_search(&params, ids)),
header(api_key.0, api_key.1),
header("Cookie", cookie_value)
)
} else {
request!(
self.web_client,
endpoint_data_search,
query(&query_parameters_data_search(&params, ids)),
header(api_key.0, api_key.1)
)
};
let resp = parse_json_response!(res, Response);
resp.into()
}
pub async fn tracking_shipment(
&self,
params: TrackingParams,
body: ShipmentRequest,
) -> LibraryResult<Vec<Shipment>> {
let api_key = crate::www::api_key_header();
let res = request_json!(
self.web_client,
endpoint_data_shipment,
body,
query(&query_parameters_data_shipment(&params)),
header(api_key.0, api_key.1)
);
let resp = parse_json_response!(res, Response);
resp.into()
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct SendungEmpfaenger {
name: String,
ort: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SendungsInfo {
gesuchte_sendungsnummer: String,
sendungsrichtung: String,
sendungsname: Option<String>,
sendungsliste: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SendungsVerlaufEvent {
pub datum: String,
ort: Option<String>,
pub ruecksendung: bool,
pub status: String,
}
impl SendungsVerlaufEvent {
fn get_ort(&self) -> Option<String> {
if let Some(ort) = self.ort.as_ref() {
if ort.len() > 0 {
return Some(ort.clone());
}
}
None
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SendungsVerlauf {
kurz_status: Option<String>,
icon_id: Option<String>,
datum_aktueller_status: Option<String>,
aktueller_status: Option<String>,
pub events: Option<Vec<SendungsVerlaufEvent>>,
farbe: u32,
pub fortschritt: u32,
pub maximal_fortschritt: u32,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SendungZustellung {
abholcode_available: Option<bool>, // probably always there
benachrichtigt_in_filiale: Option<bool>, // probably always there
zustellzeitfenster_bis: Option<String>,
zustellzeitfenster_von: Option<String>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SendungServices {
statusbenachrichtigung: SendungServiceStatusBenachrichtigung,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SendungServiceStatusBenachrichtigung {
aktueller_status: Option<bool>,
erfolgte_zustellung: Option<bool>,
geplante_zustellung: Option<bool>,
authentication_required: Option<bool>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
pub enum SendungsQuelle {
TTBRIEF,
PAKET,
SVB,
OPTIMA,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Nachhaltigkeitsstatus {
bahnpaket: bool,
co2_freie_zustellung: bool,
gg_versender: bool,
ggp_empfaenger: bool,
ggp_versender: bool,
klimafreundlicher_empfang: bool,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SendungDetails {
quelle: SendungsQuelle, // PAKET
express_sendung: Option<bool>,
is_shipper_plz: Option<bool>,
ist_zugestellt: Option<bool>,
retoure: Option<bool>,
ruecksendung: Option<bool>,
sendungsverlauf: SendungsVerlauf,
show_quality_level_hint: Option<bool>,
two_man_handling: Option<bool>,
unplausibel: Option<bool>,
mehr_informationen_verfuegbar: Option<bool>,
nachhaltigkeitsstatus: Option<Nachhaltigkeitsstatus>,
brief_sendung: Option<bool>,
invalid_time_of_day: Option<bool>,
bahnpaket: Option<bool>,
email: Option<String>,
international: Option<bool>,
pan_empfaenger: Option<SendungEmpfaenger>,
produkt_name: Option<String>,
//sendungsnummern: (),
services: Option<SendungServiceStatusBenachrichtigung>,
show_digital_notification_cta_hint: Option<bool>,
warenpost: Option<bool>,
zielland: Option<String>, // localized string?
zustellung: Option<SendungZustellung>,
}
#[derive(Debug)]
pub struct ShipmentNotFoundError {
pub no_data_available: bool,
pub not_from_dhl: bool,
pub id_invalid: bool,
pub letter_not_found: bool,
pub data_to_old: bool,
pub id_not_searchable: bool,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SendungNichtGefunden {
keine_daten_verfuegbar: Option<bool>,
keine_dhl_paket_sendung: Option<bool>,
sendungsnummer_ungueltig: Option<bool>,
brief_nicht_gefunden: Option<bool>,
sendungsdaten_zu_alt: Option<bool>,
sendungsnummer_nicht_suchbar: Option<bool>,
fehlertext: Option<String>,
fehlertextApp: Option<String>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Sendung {
id: String,
has_complete_details: bool,
sendung_nicht_gefunden: Option<SendungNichtGefunden>,
sendungsdetails: SendungDetails,
sendungsinfo: SendungsInfo,
versand_datum_benoetigt: bool,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Response {
sendungen: Option<Vec<Sendung>>,
// TODO: parse RECEIVE_MERGED_SHIPMENTS, what is that?
//merged_anonymous_shipment_list_ids: Option<Vec<()>>,
is_rate_limited: bool,
}
impl From<SendungNichtGefunden> for ShipmentNotFoundError {
fn from(value: SendungNichtGefunden) -> Self {
Self {
no_data_available: optional_default_false(value.keine_daten_verfuegbar),
not_from_dhl: optional_default_false(value.keine_dhl_paket_sendung),
id_invalid: optional_default_false(value.sendungsnummer_ungueltig),
letter_not_found: optional_default_false(value.brief_nicht_gefunden),
data_to_old: optional_default_false(value.sendungsdaten_zu_alt),
id_not_searchable: optional_default_false(value.sendungsnummer_nicht_suchbar),
}
}
}
impl From<Nachhaltigkeitsstatus> for Vec<GoGreenWashing> {
fn from(value: Nachhaltigkeitsstatus) -> Self {
let mut out = Vec::new();
if value.bahnpaket {
out.push(GoGreenWashing::ShippedByRailway);
}
if value.co2_freie_zustellung {
out.push(GoGreenWashing::CO2FreeSupposedly);
}
if value.gg_versender {
out.push(GoGreenWashing::GoGreenwashingByShipper);
}
if value.ggp_empfaenger {
out.push(GoGreenWashing::GoGreenwashingPlusByRecipient);
}
if value.ggp_versender {
out.push(GoGreenWashing::GoGreenwashingPlusByShipper);
}
if value.klimafreundlicher_empfang {
out.push(GoGreenWashing::SupposedlyClimateFriendlyReception);
}
out
}
}
enum GoGreenWashing {
ShippedByRailway,
CO2FreeSupposedly,
GoGreenwashingByShipper,
GoGreenwashingPlusByShipper,
GoGreenwashingPlusByRecipient,
SupposedlyClimateFriendlyReception,
}
#[derive(Debug)]
pub struct ShipmentSpecialDetails {
abholcode_available: bool,
benachrichtigt_in_filiale: bool,
zustellzeitfenster_bis: Option<String>,
zustellzeitfenster_von: Option<String>,
railway_shipment: Option<bool>,
express_shipment: Option<bool>,
warenpost: Option<bool>,
retoure: Option<bool>,
ruecksendung: Option<bool>,
two_man_handling: Option<bool>,
unplausibel: Option<bool>,
target_country: Option<String>,
recipient: Option<SendungEmpfaenger>,
pub product_name: Option<String>,
pub shipment_name: Option<String>,
}
#[derive(Debug)]
pub struct Shipment {
pub id: String,
pub api_has_complete_details: bool,
pub needs_shipment_date: bool,
pub needs_plz: bool,
pub quelle: SendungsQuelle,
// probably not optional
pub international: Option<bool>,
// probably not optional
pub has_shipped: Option<bool>,
pub special: ShipmentSpecialDetails,
pub history: SendungsVerlauf,
pub error: Option<ShipmentNotFoundError>,
}
fn optional_default_false(value: Option<bool>) -> bool {
if let Some(value) = value {
value
} else {
false
}
}
impl From<Sendung> for Shipment {
fn from(value: Sendung) -> Self {
let (
abholcode_available,
benachrichtigt_in_filiale,
zustellzeitfenster_bis,
zustellzeitfenster_von,
) = {
if let Some(zustellung) = value.sendungsdetails.zustellung {
(
optional_default_false(zustellung.abholcode_available),
optional_default_false(zustellung.benachrichtigt_in_filiale),
zustellung.zustellzeitfenster_bis,
zustellung.zustellzeitfenster_von,
)
} else {
(false, false, None, None)
}
};
Self {
id: value.id,
api_has_complete_details: value.has_complete_details,
needs_shipment_date: value.versand_datum_benoetigt,
needs_plz: optional_default_false(value.sendungsdetails.mehr_informationen_verfuegbar),
international: value.sendungsdetails.international,
has_shipped: value.sendungsdetails.ist_zugestellt,
quelle: value.sendungsdetails.quelle,
error: {
if let Some(err) = value.sendung_nicht_gefunden {
Some(err.into())
} else {
None
}
},
history: value.sendungsdetails.sendungsverlauf,
special: ShipmentSpecialDetails {
abholcode_available,
benachrichtigt_in_filiale,
zustellzeitfenster_bis,
zustellzeitfenster_von,
shipment_name: value.sendungsinfo.sendungsname,
railway_shipment: value.sendungsdetails.bahnpaket,
express_shipment: value.sendungsdetails.express_sendung,
warenpost: value.sendungsdetails.warenpost,
retoure: value.sendungsdetails.retoure,
ruecksendung: value.sendungsdetails.ruecksendung,
two_man_handling: value.sendungsdetails.two_man_handling,
unplausibel: value.sendungsdetails.unplausibel,
target_country: value.sendungsdetails.zielland,
recipient: value.sendungsdetails.pan_empfaenger,
product_name: value.sendungsdetails.produkt_name,
},
}
}
}
impl From<Response> for LibraryResult<Vec<Shipment>> {
fn from(value: Response) -> Self {
if value.is_rate_limited {
Err(crate::LibraryError::NetworkFetch)
} else {
let mut arr = Vec::new();
for item in value.sendungen.unwrap() {
arr.push(item.into());
}
Ok(arr)
}
}
}
fn endpoint_data_search() -> &'static str {
"https://www.dhl.de/int-verfolgen/data/search"
}
fn query_parameters_data_search(
params: &TrackingParams,
mut shippingnumbers: Vec<String>,
) -> Vec<(String, String)> {
let mut out = vec![("noRedirect".to_string(), "true".to_string())];
if let Some(lang) = params.language.as_ref() {
out.push(("language".to_string(), lang.clone()));
}
if shippingnumbers.len() > 0 {
let mut shippingnumbers_string = shippingnumbers.pop().unwrap();
for number in shippingnumbers {
shippingnumbers_string = format!(",{}", number);
}
out.push(("piececode".to_string(), shippingnumbers_string));
}
out
}
fn endpoint_data_shipment() -> &'static str {
"https://www.dhl.de/int-verfolgen/data/shipment"
}
fn query_parameters_data_shipment(params: &TrackingParams) -> Vec<(String, String)> {
let mut out = vec![];
if let Some(lang) = params.language.as_ref() {
out.push(("language".to_string(), lang.clone()));
}
out
}

View file

@ -20,8 +20,7 @@ impl CookieHeaderValueBuilder {
}
pub fn add_dhlcs(mut self, dhli: &DHLIdToken) -> Self {
self.list
.push(("dhlcs".to_string(), dhli.get_dhlcs().to_string()));
self.list.push(("dhlcs".to_string(), dhli.get_dhlcs().to_string()));
self
}
@ -55,7 +54,7 @@ macro_rules! mini_assert_api_eq {
if $a != $b {
return Err(crate::LibraryError::APIChange);
}
};
}
}
macro_rules! mini_assert_api {
@ -63,163 +62,14 @@ macro_rules! mini_assert_api {
if !$a {
return Err(crate::LibraryError::APIChange);
}
};
}
}
macro_rules! mini_assert_inval {
($a: expr) => {
if !$a {
return Err(crate::LibraryError::InvalidArgument(format!(
"MiniAssert failed: $a"
)));
return Err(crate::LibraryError::InvalidArgument(format!("MiniAssert failed: $a")));
}
};
}
macro_rules! parse_response_internal {
($res: expr) => {{
if let Err(err) = $res {
return Err(err.into());
}
let res = $res.unwrap();
res
}};
}
macro_rules! request_internal {
(
$client: tt,
$method: ident,
$endpoint: tt
$(#$func:ident$args:tt)*
) => {
{
let req = $client.$method($endpoint)
$(
.$func$args
)*
.build()
.unwrap();
let res = $client.execute(req).await;
parse_response_internal!(res)
}
};
}
macro_rules! request {
(
$self:ident.$client:ident,
$endpoint: tt,
$($func:ident$args:tt),*
) => {
request_internal!(($self.$client), get, ($endpoint()) $(#$func$args)*)
};
(
$self:ident.$client:ident,
$endpoint: ident,
$($func:ident$args:tt),*
) => {
request_internal!(($self.$client), get, $endpoint $(#$func$args)*)
};
}
macro_rules! request_post {
(
$self:ident.$client:ident,
$endpoint: ident,
$(.$func:ident$args:tt)*
) => {
request_internal!(
($self.$client), post, $endpoint $(# $func$args)*)
};
(
$self:ident.$client:ident,
$endpoint: tt,
$(.$func:ident$args:tt)*
) => {
request_internal!(
($self.$client), post, $endpoint $(# $func$args)*)
};
}
macro_rules! request_json {
(
$self:ident.$client:ident,
$endpoint: ident,
$body: tt,
$($func:ident$args:tt),*
) => {
request_json!($self.$client, ($endpoint()), $body, $( .$func$args)*)
};
(
$self:ident.$client:ident,
$endpoint: tt,
$body: tt,
$(.$func:ident$args:tt)*
) => {{
let body = serde_json::to_string(&$body).unwrap();
request_post!(
$self.$client, $endpoint,
$(
.$func$args
)*
.header("content-type", "application/json")
.header("content-length", body.len())
.body(body)
)
}};
}
macro_rules! parse_json_response {
($res: expr, $type: ty) => {{
let res = $res.text().await.unwrap();
let jd = &mut serde_json::Deserializer::from_str(res.as_str());
let mut unused = std::collections::BTreeSet::new();
println!("res({}): {}", stringify!($type), res);
let res: Result<$type,_> = serde_ignored::deserialize(jd, |path| {
unused.insert(path.to_string());
});
println!("res({}): {:?}", stringify!($type), unused);
let res: $type = match res {
Ok(res) => res,
Err(err) => return Err(crate::LibraryError::from(err)),
};
res
}};
}
macro_rules! parse_json_response_from_apiresult {
($res: expr, $type: ty) => {{
let res = $res.text().await.unwrap();
let jd = &mut serde_json::Deserializer::from_str(res.as_str());
let mut unused = std::collections::BTreeSet::new();
println!("res({}): {}", stringify!($type), res);
let res: Result<APIResult<$type>,_> = serde_ignored::deserialize(jd, |path| {
unused.insert(path.to_string());
});
println!("res({}): {:?}", stringify!($type), unused);
let res: LibraryResult<$type> = match res {
Ok(res) => res.into(),
Err(err) => return Err(crate::LibraryError::from(err)),
};
let res = match res {
Ok(res) => res,
Err(err) => return Err(crate::LibraryError::from(err)),
};
res
}};
}
}
}

View file

@ -44,10 +44,6 @@ pub(crate) fn authorized_credentials() -> (&'static str, &'static str) {
("erkennen", "8XRUfutM8PTvUz3A")
}
pub(crate) fn api_key_header() -> (&'static str, &'static str) {
("x-api-key", "a0d5b9049ba8918871e6e20bd5c49974")
}
// "/int-push/", "X-APIKey", "5{@8*nB=?\\.@t,XwWK>Y[=yY^*Y8&myzDE7_"
// /int-stammdaten/", null, "zAuoC3%7*qbRVmiXdNGyYz9iJ7N@Ph3Cw4zV"
// "/int-verfolgen/data/packstation/v2/", null, "a0d5b9049ba8918871e6e20bd5c49974",

View file

@ -16,4 +16,3 @@ reqwest = "0.12"
libpaket = { path = "../libpaket" }
glycin = { version = "2.0.0-beta", features = ["gdk4"] }
oo7 = { version = "0.3" }
relm4-icons = { version = "0.9" }

View file

@ -1,13 +1,13 @@
use adw::{gio, glib};
use gtk::gdk;
use libpaket::advices::UatToken;
use libpaket::LibraryError;
use relm4::gtk;
use gtk::gdk;
use adw::{gio, glib};
use adw::prelude::*;
use gio::prelude::*;
use glib::prelude::*;
use relm4::prelude::*;
use adw::prelude::*;
use glib::prelude::*;
use gio::prelude::*;
#[derive(Debug)]
pub struct AppAdviceMetadata {
@ -42,24 +42,16 @@ impl FactoryComponent for AppAdvice {
add_overlay = &gtk::Spinner {
start: (),
set_align: gtk::Align::Center,
#[track(self.changed_texture())]
set_visible: self.texture.is_none(),
},
add_overlay = &gtk::Box {
add_css_class: relm4::css::OSD,
add_css_class: relm4::css::TOOLBAR,
add_css_class: relm4::css::NUMERIC,
add_overlay = &gtk::Label {
set_halign: gtk::Align::Center,
set_valign: gtk::Align::Center,
set_valign: gtk::Align::End,
set_halign: gtk::Align::End,
set_margin_all: 8,
gtk::Label {
set_label: self.metadata.date.as_str(),
},
set_label: self.metadata.date.as_str(),
},
#[wrap(Some)]
@ -79,11 +71,9 @@ impl FactoryComponent for AppAdvice {
let advice = _self.metadata.advice.clone();
let uat = value.1;
sender.oneshot_command(async move {
let res = libpaket::advices::AdviceClient::new()
.fetch_advice_image(&advice, &uat)
.await;
let res = libpaket::advices::AdviceClient::new().fetch_advice_image(&advice, &uat).await;
let res = match res {
Ok(res) => res,
@ -93,21 +83,13 @@ impl FactoryComponent for AppAdvice {
let file = {
let (file, io_stream) = gio::File::new_tmp(None::<&std::path::Path>).unwrap();
let output_stream = io_stream.output_stream();
output_stream
.write(res.as_slice(), None::<&gio::Cancellable>)
.unwrap();
output_stream.write(res.as_slice(), None::<&gio::Cancellable>).unwrap();
file
};
let image = glycin::Loader::new(file)
.load()
.await
.expect("Image decoding failed");
let frame = image
.next_frame()
.await
.expect("Image frame decoding failed");
let image = glycin::Loader::new(file).load().await.expect("Image decoding failed");
let frame = image.next_frame().await.expect("Image frame decoding failed");
AppAdviceCmds::GotTexture(frame.texture())
});
@ -117,7 +99,8 @@ impl FactoryComponent for AppAdvice {
fn update_cmd(&mut self, message: Self::CommandOutput, sender: FactorySender<Self>) {
match message {
AppAdviceCmds::GotTexture(texture) => self.set_texture(Some(texture)),
AppAdviceCmds::Error(err) => todo!(),
AppAdviceCmds::Error(err) => todo!()
};
}
}
}

View file

@ -128,28 +128,24 @@ impl AsyncComponent for Login {
.expect("sender not worky");
} else {
match keyring
.search_items(&HashMap::from(KEYRING_ATTRIBUTES))
.await
{
.search_items(&HashMap::from(KEYRING_ATTRIBUTES))
.await {
Ok(res) => {
if res.len() > 0 {
let item = &res[0];
let refresh_token = item.secret().await.unwrap();
let refresh_token =
std::str::from_utf8(refresh_token.as_slice()).unwrap();
model.refresh_token = Some(
RefreshToken::new(refresh_token.to_string()).unwrap(),
);
let refresh_token = std::str::from_utf8(refresh_token.as_slice()).unwrap();
model.refresh_token = Some(RefreshToken::new(refresh_token.to_string()).unwrap());
sender.input(LoginInput::NeedsRefresh);
} else {
sender.input(LoginInput::NeedsLogin);
}
}
},
Err(err) => {
sender
.output(LoginOutput::KeyringError(err))
.expect("sender not worky");
}
.output(LoginOutput::KeyringError(err))
.expect("sender not worky");
},
};
}
}
@ -212,9 +208,7 @@ impl AsyncComponent for Login {
}
LoginInput::BreakWorld => {
self.set_state(LoginState::Offline);
sender
.output(LoginOutput::Error(libpaket::LibraryError::APIChange))
.unwrap();
sender.output(LoginOutput::Error(libpaket::LibraryError::APIChange)).unwrap();
{
let shared_id_token = self.shared_id_token.lock().await;
let mut shared_id_token = shared_id_token.write();
@ -300,15 +294,7 @@ impl Login {
let future = async {
self.refresh_token = Some(res.refresh_token);
let keyring = KEYRING.get().unwrap();
keyring
.create_item(
"Refresh Token",
&HashMap::from(KEYRING_ATTRIBUTES),
self.refresh_token.as_ref().unwrap().to_string(),
true,
)
.await
.unwrap();
keyring.create_item("Refresh Token", &HashMap::from(KEYRING_ATTRIBUTES), self.refresh_token.as_ref().unwrap().to_string(), true).await.unwrap();
};
if !res.id_token.is_expired() {
@ -337,42 +323,62 @@ impl Login {
libpaket::LibraryError::Unauthorized => {
sender.input(LoginInput::NeedsLogin);
}
libpaket::LibraryError::Deprecated => {
sender.output(LoginOutput::Error(res)).unwrap();
sender.input(LoginInput::BreakWorld);
}
}
}
}
}
}
macro_rules! received {
($func_name:ident, $args:tt, $calling:ident, $calling_args:tt) => {
async fn $func_name$args -> LoginCommand {
let client = OpenIdClient::new();
let mut err = LibraryError::NetworkFetch;
for _ in 0..6 {
let result: Result<TokenResponse, LibraryError> = client
.$calling$calling_args
.await;
err = match result {
Ok(result) => return LoginCommand::Token(Ok(result)),
Err(err) => err,
};
if err == LibraryError::NetworkFetch {
continue;
} else {
return LoginCommand::Token(Err(err));
}
}
LoginCommand::Token(Err(err))
}
};
fn convert_library_error_to_response_type(err: &LibraryError) -> ResponseType {
match err {
LibraryError::NetworkFetch => ResponseType::Retry,
LibraryError::Unauthorized => ResponseType::Okay,
LibraryError::InvalidArgument(_) => ResponseType::Okay,
LibraryError::DecodeError(_) => ResponseType::Okay,
LibraryError::APIChange => ResponseType::Okay,
}
}
received!(received_auth_code, (auth_code: String, code_verifier: CodeVerfier), token_authorization, (auth_code.clone(), &code_verifier));
received!(use_refresh_token, (refresh_token: RefreshToken), token_refresh, (&refresh_token));
async fn received_auth_code(auth_code: String, code_verifier: CodeVerfier) -> LoginCommand {
let client = OpenIdClient::new();
let mut err = LibraryError::NetworkFetch;
for _ in 0..6 {
let result: Result<TokenResponse, LibraryError> = client
.token_authorization(auth_code.clone(), &code_verifier)
.await;
if let Ok(result) = result {
return LoginCommand::Token(Ok(result))
}
err = result.unwrap_err();
let response_type = convert_library_error_to_response_type(&err);
if response_type == ResponseType::Retry {
continue;
} else {
return LoginCommand::Token(Err(err));
}
}
LoginCommand::Token(Err(err))
}
async fn use_refresh_token(refresh_token: RefreshToken) -> LoginCommand {
let client = OpenIdClient::new();
let mut err = LibraryError::NetworkFetch;
for _ in 0..6 {
let result: Result<TokenResponse, LibraryError> =
client.token_refresh(&refresh_token).await;
err = match result {
Ok(result) => return LoginCommand::Token(Ok(result)),
Err(err) => err,
};
let response_type = convert_library_error_to_response_type(&err);
if response_type == ResponseType::Retry {
continue;
} else {
return LoginCommand::Token(Err(err));
}
}
LoginCommand::Token(Err(err))
}

View file

@ -3,7 +3,6 @@ use std::sync::Arc;
use login::{Login, LoginOutput, LoginSharedState};
use ready::{Ready, ReadyOutput};
use relm4::{
RELM_THREADS,
adw, gtk, main_adw_application, prelude::*, tokio::sync::Mutex,
AsyncComponentSender, SharedState,
};
@ -14,7 +13,6 @@ mod advices;
mod constants;
mod login;
mod ready;
mod tracking;
#[derive(Debug, PartialEq)]
enum AppState {
@ -284,19 +282,11 @@ fn convert_ready_response(response: ReadyOutput) -> AppInput {
long: "There is no feature on your account which is supported by this app. You need the offical app and register for one or more of:\n\"Briefasnkündigung\"".to_string(),
}),
ReadyOutput::Error(err) => AppInput::ErrorOccoured(AppError { short: "meow".to_string(), long: err.to_string() }),
ReadyOutput::Notification(value) => AppInput::Notification(value, 60),
ReadyOutput::Ready => AppInput::SwitchToReady,
}
}
fn main() {
RELM_THREADS.set(4).unwrap();
gtk::init().unwrap();
let display = gtk::gdk::Display::default().unwrap();
let theme = gtk::IconTheme::for_display(&display);
theme.add_resource_path("/de/j4ne/Paket/icons/");
theme.add_resource_path("/de/j4ne/Paket/scalable/actions/");
relm4_icons::initialize_icons();
let app = RelmApp::new(constants::APP_ID);
app.run_async::<App>(());
}

View file

@ -1,21 +1,14 @@
// managed the various pages...
use std::hash::DefaultHasher;
use std::string;
use std::time::Duration;
use adw::prelude::*;
use libpaket::{
self,
advices::{AdvicesList, UatToken},
tracking::{Shipment, TrackingParams},
LibraryError, LibraryResult,
};
use relm4::{
adw,
factory::{FactoryHashMap, FactorySender, FactoryVecDeque},
prelude::*,
};
use relm4::{adw, factory::FactoryVecDeque, prelude::*};
use crate::advices::AppAdviceMetadata;
@ -32,13 +25,9 @@ pub struct Ready {
login: crate::LoginSharedState,
activate: bool,
have_service_advices: bool,
#[do_not_track]
advices_factory: FactoryVecDeque<crate::advices::AppAdvice>,
advices_state: ReadyAdvicesState,
#[do_not_track]
tracking_factory: FactoryHashMap<String, crate::tracking::ShipmentView>,
}
#[derive(Debug)]
@ -46,7 +35,6 @@ pub enum ReadyOutput {
Ready,
Error(LibraryError),
FatalError(LibraryError),
Notification(String),
NoServicesEnabled,
}
@ -57,7 +45,6 @@ pub enum ReadyCmds {
GotCustomerDataFull(LibraryResult<libpaket::stammdaten::CustomerDataFull>),
RetryAdvices,
GotAdvices((LibraryResult<Vec<AppAdviceMetadata>>, Option<UatToken>)),
GotTracking(LibraryResult<Vec<Shipment>>),
}
#[derive(Debug)]
@ -65,8 +52,6 @@ pub enum ReadyInput {
Activate,
Deactivate,
HaveAdvicesService,
HavePaketankuendigungService,
SearchTracking(String),
}
#[relm4::component(pub)]
@ -92,23 +77,18 @@ impl Component for Ready {
set_title: "No mail notifications available."
},
#[name = "advices_page_have_some"]
add = &adw::Clamp {
#[wrap(Some)]
set_child = &gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
add = &gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
#[local_ref]
advices_carousel -> adw::Carousel {
set_orientation: gtk::Orientation::Vertical,
#[local_ref]
advices_carousel -> adw::Carousel {
set_orientation: gtk::Orientation::Vertical,
},
adw::CarouselIndicatorDots {
#[watch]
set_carousel: Some(advices_carousel),
set_orientation: gtk::Orientation::Vertical,
}
},
adw::CarouselIndicatorDots {
set_carousel: Some(advices_carousel),
}
},
#[track(model.changed_advices_state())]
@ -123,44 +103,9 @@ impl Component for Ready {
},
} -> /*page_advices: adw::ViewStackPage*/ {
set_title: Some("Mail notification"),
set_name: Some("page_advices"),
/*#[track(model.changed_have_service_advices())]
set_visible: model.have_service_advices,*/
},
add = &adw::Bin {
#[wrap(Some)]
set_child = &gtk::ScrolledWindow {
#[wrap(Some)]
set_child = &gtk::Box {
set_orientation: gtk::Orientation::Vertical,
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
set_margin_all: 8,
add_css_class: relm4::css::TOOLBAR,
#[name = "tracking_entry"]
gtk::Entry {
set_input_hints: gtk::InputHints::PRIVATE,
set_hexpand: true,
},
#[name = "tracking_entry_button"]
gtk::Button {}
},
#[local_ref]
tracking_box -> gtk::Box {
set_spacing: 8
},
}
}
} -> /*page_tracking: adw::ViewStackPage*/ {
set_title: Some("Shipment tracking"),
set_name: Some("page_tracking"),
},
}
}
}
@ -169,9 +114,9 @@ impl Component for Ready {
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let advices_factory = FactoryVecDeque::builder().launch_default().detach();
let tracking_factory = FactoryHashMap::builder().launch_default().detach();
let advices_factory = FactoryVecDeque::builder()
.launch(adw::Carousel::new())
.detach();
let model = Ready {
have_service_advices: false,
@ -179,12 +124,10 @@ impl Component for Ready {
advices_state: ReadyAdvicesState::Loading,
login: init.clone(),
activate: false,
tracking_factory,
tracker: 0,
};
let advices_carousel = model.advices_factory.widget();
let tracking_box = model.tracking_factory.widget();
let widgets = view_output!();
{
@ -205,21 +148,6 @@ impl Component for Ready {
.drop_on_shutdown()
});
}
{
let sender = sender.clone();
widgets.tracking_entry.connect_activate(move |entry| {
sender.input(ReadyInput::SearchTracking(entry.text().into()));
entry.set_text("");
});
}
{
let sender = sender.clone();
let entry = widgets.tracking_entry.clone();
widgets.tracking_entry_button.connect_clicked(move |_| {
sender.input(ReadyInput::SearchTracking(entry.text().into()));
entry.set_text("");
});
}
ComponentParts { model, widgets }
}
@ -227,11 +155,11 @@ impl Component for Ready {
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>, _: &Self::Root) {
self.reset();
let token = self.login.clone();
match message {
ReadyInput::Activate => {
self.set_activate(true);
if self.changed_activate() {
let token = self.login.clone();
sender.oneshot_command(async move {
let token = crate::login::get_id_token(&token).await.unwrap();
let client = libpaket::StammdatenClient::new();
@ -239,48 +167,10 @@ impl Component for Ready {
});
}
}
ReadyInput::SearchTracking(value) => {
sender.oneshot_command(async move {
let client = libpaket::WebClient::new();
let mut vec = Vec::new();
vec.push(value);
ReadyCmds::GotTracking(
client
.tracking_search(
TrackingParams {
language: Some("de".to_string()),
},
vec,
None,
)
.await,
)
});
}
ReadyInput::Deactivate => {
self.set_activate(false);
}
ReadyInput::HavePaketankuendigungService => {
let token = self.login.clone();
sender.oneshot_command(async move {
// fetching advices
let dhli_token = crate::login::get_id_token(&token).await.unwrap();
let client = libpaket::WebClient::new();
ReadyCmds::GotTracking(
client
.tracking_search(
TrackingParams {
language: Some("de".to_string()),
},
Vec::new(),
Some(&dhli_token),
)
.await,
)
});
}
ReadyInput::HaveAdvicesService => {
let token = self.login.clone();
sender.oneshot_command(async move {
// fetching advices
let dhli_token = crate::login::get_id_token(&token).await.unwrap();
@ -327,81 +217,27 @@ impl Component for Ready {
ReadyCmds::LoggedOut => sender.input(ReadyInput::Deactivate),
ReadyCmds::GotCustomerDataFull(res) => match res {
Ok(res) => {
let mut a_service_was_activated = false;
for service in &res.common.services {
match service {
libpaket::stammdaten::CustomerDataService::Packstation => (),
libpaket::stammdaten::CustomerDataService::Paketankuendigung => {
sender.input(ReadyInput::HavePaketankuendigungService);
}
libpaket::stammdaten::CustomerDataService::Paketankuendigung => (),
libpaket::stammdaten::CustomerDataService::PostfilialeDirekt => (),
libpaket::stammdaten::CustomerDataService::Digiben => (),
libpaket::stammdaten::CustomerDataService::GeraetAktiviert => (),
libpaket::stammdaten::CustomerDataService::Briefankuendigung => {
a_service_was_activated = true;
sender.input(ReadyInput::HaveAdvicesService);
}
}
}
sender.output(ReadyOutput::Ready).unwrap()
}
Err(err) => sender.output(ReadyOutput::FatalError(err)).unwrap(),
},
ReadyCmds::GotTracking(res) => match res {
Ok(shipment_vec) => {
for item in shipment_vec {
if let Some(err) = item.error {
// TODO: gettext
if err.id_invalid {
sender
.output(ReadyOutput::Notification(format!(
"The id is invalid ({})",
item.id
)))
.unwrap();
} else if err.letter_not_found {
sender
.output(ReadyOutput::Notification(format!(
"The letter wasn't found ({})",
item.id
)))
.unwrap();
} else if err.id_not_searchable {
sender
.output(ReadyOutput::Notification(format!(
"The id is not searchable ({})",
item.id
)))
.unwrap();
} else if err.data_to_old {
sender
.output(ReadyOutput::Notification(format!(
"No data available with id ({}) (data expired)",
item.id
)))
.unwrap();
} else if err.not_from_dhl {
sender
.output(ReadyOutput::Notification(format!(
"The id is not from DHL ({})",
item.id
)))
.unwrap();
} else if err.no_data_available {
sender
.output(ReadyOutput::Notification(format!(
"No data available with id ({})",
item.id
)))
.unwrap();
}
} else {
self.tracking_factory.insert(item.id.clone(), item);
}
if a_service_was_activated {
sender.output(ReadyOutput::Ready).unwrap()
} else {
sender.output(ReadyOutput::NoServicesEnabled).unwrap();
}
}
Err(err) => {
sender.output(ReadyOutput::Error(err)).unwrap();
}
Err(err) => sender.output(ReadyOutput::FatalError(err)).unwrap(),
},
ReadyCmds::GotAdvices(res) => match res.0 {
Ok(advices_vec) => {

View file

@ -1,193 +0,0 @@
use adw::prelude::*;
use libpaket::tracking::Shipment;
use relm4::factory::FactoryComponent;
use relm4::prelude::*;
use relm4::{adw, gtk};
pub struct ShipmentView {
model: Shipment,
// model abstraction
have_events: bool,
// state
expanded: bool,
// workarounds
list_box_history: gtk::ListBox,
}
#[derive(Debug)]
pub enum ViewInput {
ToggleExpand,
}
#[relm4::factory(pub)]
impl FactoryComponent for ShipmentView {
type CommandOutput = ();
type Init = Shipment;
type Output = ();
type Input = ViewInput;
type ParentWidget = gtk::Box;
type Index = String;
view! {
#[root]
gtk::Box {
add_css_class: relm4::css::CARD,
set_hexpand: true,
set_margin_all: 8,
set_orientation: gtk::Orientation::Vertical,
gtk::ProgressBar {
add_css_class: relm4::css::OSD,
set_margin_start: 8,
set_margin_end: 8,
set_margin_bottom: 1,
set_fraction: f64::from(self.model.history.maximal_fortschritt) / f64::from(self.model.history.fortschritt),
},
// title box
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
set_hexpand: true,
set_margin_all: 8,
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_hexpand: true,
set_halign: gtk::Align::Start,
gtk::Label {
add_css_class: relm4::css::NUMERIC,
add_css_class: relm4::css::CAPTION_HEADING,
set_halign: gtk::Align::Start,
set_label: &self.model.id,
},
gtk::Label {
add_css_class: relm4::css::HEADING,
set_halign: gtk::Align::Start,
set_label: {
// TODO: gettext
if let Some(shipment_name) = &self.model.special.shipment_name {
shipment_name.as_str()
} else if let Some(product_name) = &self.model.special.product_name {
product_name.as_str()
} else {
match self.model.quelle {
libpaket::tracking::SendungsQuelle::TTBRIEF => "Letter",
libpaket::tracking::SendungsQuelle::PAKET => "Parcel",
libpaket::tracking::SendungsQuelle::SVB => "quelle: SVB",
libpaket::tracking::SendungsQuelle::OPTIMA => "quelle: OPTIMA",
}
}
}
}
},
gtk::Button {
set_halign: gtk::Align::End,
add_css_class: relm4::css::FLAT,
#[watch]
set_icon_name: {
if self.expanded {
relm4_icons::icon_names::MINUS
} else {
relm4_icons::icon_names::PLUS
}
},
connect_clicked => ViewInput::ToggleExpand,
}
}, // title box end
gtk::Revealer {
#[watch]
set_reveal_child: self.expanded,
#[wrap(Some)]
set_child = &gtk::Box {
// history viewstack
adw::StatusPage {
set_visible: !self.have_events,
set_title: "No events",
},
append = &self.list_box_history.clone() {
set_visible: self.have_events,
add_css_class: relm4::css::BOXED_LIST,
set_selection_mode: gtk::SelectionMode::None,
},
}
},
}
}
fn init_model(
init: Self::Init,
index: &Self::Index,
sender: relm4::FactorySender<Self>,
) -> Self {
let have_events = init.history.events.as_ref().is_some_and(|a| a.len() > 0);
let list_box_history = gtk::ListBox::new();
let _self = ShipmentView {
have_events,
model: init,
list_box_history,
expanded: false,
};
for elem in _self.model.history.events.as_ref().unwrap() {
let label_datum = gtk::Label::builder()
.css_classes([relm4::css::NUMERIC])
.label(&elem.datum)
.halign(gtk::Align::Start)
.valign(gtk::Align::Start)
.build();
// TODO: is html, parse it
let label_status = gtk::Label::builder()
.wrap(true)
.halign(gtk::Align::Start)
.valign(gtk::Align::Start)
.build();
label_status.set_markup(&elem.status);
let boxie = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.margin_start(8)
.margin_end(8)
.spacing(8)
.build();
boxie.append(&label_datum);
boxie.append(&label_status);
_self.list_box_history.append(&boxie);
}
_self
}
fn update(&mut self, message: Self::Input, sender: FactorySender<Self>) {
match message {
ViewInput::ToggleExpand => {
self.expanded = !self.expanded;
}
}
}
}