1/*
2 * Copyright 2024, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5#include "ScreenshotCoordinate.h"
6
7
8static const char* kCodeKey = "code";
9static const char* kWidthKey = "width";
10static const char* kHeightKey = "height";
11
12
13ScreenshotCoordinate::ScreenshotCoordinate()
14	:
15	fCode(""),
16	fWidth(0),
17	fHeight(0)
18{
19}
20
21
22ScreenshotCoordinate::ScreenshotCoordinate(const BMessage* from)
23{
24	from->FindString(kCodeKey, &fCode);
25	from->FindUInt16(kWidthKey, &fWidth);
26	from->FindUInt16(kHeightKey, &fHeight);
27}
28
29
30ScreenshotCoordinate::ScreenshotCoordinate(BString code, uint16 width, uint16 height)
31	:
32	fCode(code),
33	fWidth(width),
34	fHeight(height)
35{
36}
37
38
39ScreenshotCoordinate::~ScreenshotCoordinate()
40{
41}
42
43
44const BString
45ScreenshotCoordinate::Code() const
46{
47	return fCode;
48}
49
50
51uint16
52ScreenshotCoordinate::Width() const
53{
54	return fWidth;
55}
56
57
58uint16
59ScreenshotCoordinate::Height() const
60{
61	return fHeight;
62}
63
64
65bool
66ScreenshotCoordinate::IsValid() const
67{
68	return !fCode.IsEmpty() && fWidth > 0 && fHeight > 0;
69}
70
71
72bool
73ScreenshotCoordinate::operator==(const ScreenshotCoordinate& other) const
74{
75	return fCode == other.fCode && fHeight == other.fHeight && fWidth == other.fWidth;
76}
77
78
79const BString
80ScreenshotCoordinate::Key() const
81{
82	BString result;
83	result.SetToFormat("%s_%" B_PRIu16 "x%" B_PRIu16 , fCode.String(), fWidth, fHeight);
84	return result;
85}
86
87
88const BString
89ScreenshotCoordinate::CacheFilename() const
90{
91	return BString() << Key() << ".png";
92}
93
94
95status_t
96ScreenshotCoordinate::Archive(BMessage* into, bool deep) const
97{
98	status_t result = B_OK;
99	if (result == B_OK)
100		result = into->AddString(kCodeKey, fCode);
101	if (result == B_OK)
102		result = into->AddUInt16(kWidthKey, fWidth);
103	if (result == B_OK)
104		result = into->AddUInt16(kHeightKey, fHeight);
105	return result;
106}
107