1 /*
2 * Copyright 2009-2010, Stephan Aßmus <superstippi@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6 #include "ProgressReporter.h"
7
8 #include <stdio.h>
9
10 #include <Catalog.h>
11 #include <Locale.h>
12
13
14 #undef B_TRANSLATION_CONTEXT
15 #define B_TRANSLATION_CONTEXT "InstallProgress"
16
17
ProgressReporter(const BMessenger & messenger,BMessage * message)18 ProgressReporter::ProgressReporter(const BMessenger& messenger,
19 BMessage* message)
20 :
21 fStartTime(0),
22
23 fBytesToWrite(0),
24 fBytesWritten(0),
25
26 fItemsToWrite(0),
27 fItemsWritten(0),
28
29 fMessenger(messenger),
30 fMessage(message)
31 {
32 }
33
34
~ProgressReporter()35 ProgressReporter::~ProgressReporter()
36 {
37 delete fMessage;
38 }
39
40
41 void
Reset()42 ProgressReporter::Reset()
43 {
44 fBytesToWrite = 0;
45 fBytesWritten = 0;
46
47 fItemsToWrite = 0;
48 fItemsWritten = 0;
49
50 if (fMessage) {
51 BMessage message(*fMessage);
52 message.AddString("status",
53 B_TRANSLATE("Collecting copy information."));
54 fMessenger.SendMessage(&message);
55 }
56 }
57
58
59 void
AddItems(uint64 count,off_t bytes)60 ProgressReporter::AddItems(uint64 count, off_t bytes)
61 {
62 fBytesToWrite += bytes;
63 fItemsToWrite += count;
64 }
65
66
67 void
StartTimer()68 ProgressReporter::StartTimer()
69 {
70 fStartTime = system_time();
71
72 printf("%" B_PRIdOFF " bytes to write in %" B_PRId64 " files\n",
73 fBytesToWrite, fItemsToWrite);
74
75 if (fMessage) {
76 BMessage message(*fMessage);
77 message.AddString("status", B_TRANSLATE("Performing installation."));
78 fMessenger.SendMessage(&message);
79 }
80 }
81
82
83 void
ItemsWritten(uint64 items,off_t bytes,const char * itemName,const char * targetFolder)84 ProgressReporter::ItemsWritten(uint64 items, off_t bytes,
85 const char* itemName, const char* targetFolder)
86 {
87 fItemsWritten += items;
88 fBytesWritten += bytes;
89
90 _UpdateProgress(itemName, targetFolder);
91 }
92
93
94 void
_UpdateProgress(const char * itemName,const char * targetFolder)95 ProgressReporter::_UpdateProgress(const char* itemName,
96 const char* targetFolder)
97 {
98 if (fMessage == NULL)
99 return;
100
101 // TODO: Could add time to finish calculation here...
102
103 BMessage message(*fMessage);
104 float progress = 100.0 * fBytesWritten / fBytesToWrite;
105 message.AddFloat("progress", progress);
106 message.AddInt32("current", fItemsWritten);
107 message.AddInt32("maximum", fItemsToWrite);
108 message.AddString("item", itemName);
109 message.AddString("folder", targetFolder);
110 fMessenger.SendMessage(&message);
111 }
112