xref: /haiku/src/apps/installer/ProgressReporter.cpp (revision 72586154d7f505bd5f15377db729b515d40dee40)
1 /*
2  * Copyright 2009, 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 
11 ProgressReporter::ProgressReporter(const BMessenger& messenger,
12 		BMessage* message)
13 	:
14 	fStartTime(0),
15 
16 	fBytesToWrite(0),
17 	fBytesWritten(0),
18 
19 	fItemsToWrite(0),
20 	fItemsWritten(0),
21 
22 	fMessenger(messenger),
23 	fMessage(message)
24 {
25 }
26 
27 
28 ProgressReporter::~ProgressReporter()
29 {
30 	delete fMessage;
31 }
32 
33 
34 void
35 ProgressReporter::Reset()
36 {
37 	fBytesToWrite = 0;
38 	fBytesWritten = 0;
39 
40 	fItemsToWrite = 0;
41 	fItemsWritten = 0;
42 
43 	if (fMessage) {
44 		BMessage message(*fMessage);
45 		message.AddString("status", "Collecting copy information.");
46 		fMessenger.SendMessage(&message);
47 	}
48 }
49 
50 
51 void
52 ProgressReporter::AddItems(uint64 count, off_t bytes)
53 {
54 	fBytesToWrite += bytes;
55 	fItemsToWrite += count;
56 }
57 
58 
59 void
60 ProgressReporter::StartTimer()
61 {
62 	fStartTime = system_time();
63 
64 	printf("%lld bytes to write in %lld files\n", fBytesToWrite,
65 		fItemsToWrite);
66 
67 	if (fMessage) {
68 		BMessage message(*fMessage);
69 		message.AddString("status", "Performing installation.");
70 		fMessenger.SendMessage(&message);
71 	}
72 }
73 
74 
75 void
76 ProgressReporter::ItemsWritten(uint64 items, off_t bytes,
77 	const char* itemName, const char* targetFolder)
78 {
79 	fItemsWritten += items;
80 	fBytesWritten += bytes;
81 
82 	_UpdateProgress(itemName, targetFolder);
83 }
84 
85 
86 void
87 ProgressReporter::_UpdateProgress(const char* itemName,
88 	const char* targetFolder)
89 {
90 	if (fMessage == NULL)
91 		return;
92 
93 	// TODO: Could add time to finish calculation here...
94 
95 	BMessage message(*fMessage);
96 	float progress = 100.0 * fBytesWritten / fBytesToWrite;
97 	message.AddFloat("progress", progress);
98 	message.AddInt32("current", fItemsWritten);
99 	message.AddInt32("maximum", fItemsToWrite);
100 	message.AddString("item", itemName);
101 	message.AddString("folder", targetFolder);
102 	fMessenger.SendMessage(&message);
103 }
104