xref: /haiku/src/apps/haikudepot/model/ScreenshotCoordinate.cpp (revision dd2a1e350b303b855a50fd64e6cb55618be1ae6a)
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 
8 static const char* kCodeKey = "code";
9 static const char* kWidthKey = "width";
10 static const char* kHeightKey = "height";
11 
12 
13 ScreenshotCoordinate::ScreenshotCoordinate()
14 	:
15 	fCode(""),
16 	fWidth(0),
17 	fHeight(0)
18 {
19 }
20 
21 
22 ScreenshotCoordinate::ScreenshotCoordinate(const BMessage* from)
23 {
24 	from->FindString(kCodeKey, &fCode);
25 	from->FindUInt16(kWidthKey, &fWidth);
26 	from->FindUInt16(kHeightKey, &fHeight);
27 }
28 
29 
30 ScreenshotCoordinate::ScreenshotCoordinate(BString code, uint16 width, uint16 height)
31 	:
32 	fCode(code),
33 	fWidth(width),
34 	fHeight(height)
35 {
36 }
37 
38 
39 ScreenshotCoordinate::~ScreenshotCoordinate()
40 {
41 }
42 
43 
44 const BString
45 ScreenshotCoordinate::Code() const
46 {
47 	return fCode;
48 }
49 
50 
51 uint16
52 ScreenshotCoordinate::Width() const
53 {
54 	return fWidth;
55 }
56 
57 
58 uint16
59 ScreenshotCoordinate::Height() const
60 {
61 	return fHeight;
62 }
63 
64 
65 bool
66 ScreenshotCoordinate::IsValid() const
67 {
68 	return !fCode.IsEmpty() && fWidth > 0 && fHeight > 0;
69 }
70 
71 
72 bool
73 ScreenshotCoordinate::operator==(const ScreenshotCoordinate& other) const
74 {
75 	return fCode == other.fCode && fHeight == other.fHeight && fWidth == other.fWidth;
76 }
77 
78 
79 const BString
80 ScreenshotCoordinate::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 
88 const BString
89 ScreenshotCoordinate::CacheFilename() const
90 {
91 	return BString() << Key() << ".png";
92 }
93 
94 
95 status_t
96 ScreenshotCoordinate::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