Refactor similar UpdateDialogGtk::Message and UpdateDialogWin32::Message class into a single shared UpdateMessage class.

This commit is contained in:
Robert Knight 2011-08-24 10:17:57 +01:00
parent 0ca5928f08
commit 61e7561615
5 changed files with 59 additions and 60 deletions

View file

@ -73,21 +73,22 @@ void UpdateDialogGtk::finish(GtkWidget* widget, gpointer _dialog)
gboolean UpdateDialogGtk::notify(void* _message) gboolean UpdateDialogGtk::notify(void* _message)
{ {
Message* message = static_cast<Message*>(_message); UpdateMessage* message = static_cast<UpdateMessage*>(_message);
UpdateDialogGtk* dialog = static_cast<UpdateDialogGtk*>(message->receiver);
switch (message->type) switch (message->type)
{ {
case Message::UpdateProgress: case UpdateMessage::UpdateProgress:
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(message->dialog->m_progressBar),message->progress/100.0); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dialog->m_progressBar),message->progress/100.0);
break; break;
case Message::UpdateFailed: case UpdateMessage::UpdateFailed:
gtk_label_set_text(GTK_LABEL(message->dialog->m_progressLabel), gtk_label_set_text(GTK_LABEL(dialog->m_progressLabel),
("There was a problem installing the update: " + message->message).c_str());; ("There was a problem installing the update: " + message->message).c_str());;
gtk_widget_set_sensitive(message->dialog->m_finishButton,true); gtk_widget_set_sensitive(dialog->m_finishButton,true);
break; break;
case Message::UpdateFinished: case UpdateMessage::UpdateFinished:
gtk_label_set_text(GTK_LABEL(message->dialog->m_progressLabel), gtk_label_set_text(GTK_LABEL(dialog->m_progressLabel),
"Update installed. Click 'Finish' to restart the application."); "Update installed. Click 'Finish' to restart the application.");
gtk_widget_set_sensitive(message->dialog->m_finishButton,true); gtk_widget_set_sensitive(dialog->m_finishButton,true);
break; break;
} }
delete message; delete message;
@ -99,7 +100,7 @@ gboolean UpdateDialogGtk::notify(void* _message)
// callbacks during update installation // callbacks during update installation
void UpdateDialogGtk::updateError(const std::string& errorMessage) void UpdateDialogGtk::updateError(const std::string& errorMessage)
{ {
Message* message = new Message(this,Message::UpdateFailed); UpdateMessage* message = new UpdateMessage(this,UpdateMessage::UpdateFailed);
message->message = errorMessage; message->message = errorMessage;
g_idle_add(&UpdateDialogGtk::notify,message); g_idle_add(&UpdateDialogGtk::notify,message);
} }
@ -111,14 +112,14 @@ bool UpdateDialogGtk::updateRetryCancel(const std::string& message)
void UpdateDialogGtk::updateProgress(int percentage) void UpdateDialogGtk::updateProgress(int percentage)
{ {
Message* message = new Message(this,Message::UpdateProgress); UpdateMessage* message = new UpdateMessage(this,UpdateMessage::UpdateProgress);
message->progress = percentage; message->progress = percentage;
g_idle_add(&UpdateDialogGtk::notify,message); g_idle_add(&UpdateDialogGtk::notify,message);
} }
void UpdateDialogGtk::updateFinished() void UpdateDialogGtk::updateFinished()
{ {
Message* message = new Message(this,Message::UpdateFinished); UpdateMessage* message = new UpdateMessage(this,UpdateMessage::UpdateFinished);
g_idle_add(&UpdateDialogGtk::notify,message); g_idle_add(&UpdateDialogGtk::notify,message);
} }

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "UpdateMessage.h"
#include "UpdateObserver.h" #include "UpdateObserver.h"
#include <gtk/gtk.h> #include <gtk/gtk.h>
@ -22,27 +23,6 @@ class UpdateDialogGtk : public UpdateObserver
virtual void updateFinished(); virtual void updateFinished();
private: private:
struct Message
{
enum Type
{
UpdateFailed,
UpdateProgress,
UpdateFinished
};
Message(UpdateDialogGtk* _dialog, Type _type)
: dialog(_dialog)
, type(_type)
{
}
UpdateDialogGtk* dialog;
Type type;
std::string message;
int progress;
};
static void finish(GtkWidget* widget, gpointer dialog); static void finish(GtkWidget* widget, gpointer dialog);
static gboolean notify(void* message); static gboolean notify(void* message);

View file

@ -124,7 +124,7 @@ void UpdateDialogWin32::exec()
void UpdateDialogWin32::updateError(const std::string& errorMessage) void UpdateDialogWin32::updateError(const std::string& errorMessage)
{ {
Message* message = new Message(Message::UpdateFailed); UpdateMessage* message = new UpdateMessage(UpdateMessage::UpdateFailed);
message->message = errorMessage; message->message = errorMessage;
SendNotifyMessage(m_window.GetHwnd(),WM_USER,reinterpret_cast<WPARAM>(message),0); SendNotifyMessage(m_window.GetHwnd(),WM_USER,reinterpret_cast<WPARAM>(message),0);
} }
@ -136,14 +136,14 @@ bool UpdateDialogWin32::updateRetryCancel(const std::string& message)
void UpdateDialogWin32::updateProgress(int percentage) void UpdateDialogWin32::updateProgress(int percentage)
{ {
Message* message = new Message(Message::UpdateProgress); UpdateMessage* message = new UpdateMessage(UpdateMessage::UpdateProgress);
message->progress = percentage; message->progress = percentage;
SendNotifyMessage(m_window.GetHwnd(),WM_USER,reinterpret_cast<WPARAM>(message),0); SendNotifyMessage(m_window.GetHwnd(),WM_USER,reinterpret_cast<WPARAM>(message),0);
} }
void UpdateDialogWin32::updateFinished() void UpdateDialogWin32::updateFinished()
{ {
Message* message = new Message(Message::UpdateFinished); UpdateMessage* message = new UpdateMessage(UpdateMessage::UpdateFinished);
SendNotifyMessage(m_window.GetHwnd(),WM_USER,reinterpret_cast<WPARAM>(message),0); SendNotifyMessage(m_window.GetHwnd(),WM_USER,reinterpret_cast<WPARAM>(message),0);
} }
@ -169,10 +169,10 @@ LRESULT WINAPI UpdateDialogWin32::windowProc(HWND window, UINT message, WPARAM w
{ {
if (window == m_window.GetHwnd()) if (window == m_window.GetHwnd())
{ {
Message* message = reinterpret_cast<Message*>(wParam); UpdateMessage* message = reinterpret_cast<UpdateMessage*>(wParam);
switch (message->type) switch (message->type)
{ {
case Message::UpdateFailed: case UpdateMessage::UpdateFailed:
{ {
m_hadError = true; m_hadError = true;
std::string text = "There was a problem installing the update:\n\n" + std::string text = "There was a problem installing the update:\n\n" +
@ -180,10 +180,10 @@ LRESULT WINAPI UpdateDialogWin32::windowProc(HWND window, UINT message, WPARAM w
MessageBox(m_window.GetHwnd(),text.c_str(),"Update Problem",MB_OK); MessageBox(m_window.GetHwnd(),text.c_str(),"Update Problem",MB_OK);
} }
break; break;
case Message::UpdateProgress: case UpdateMessage::UpdateProgress:
m_progressBar.SetPos(message->progress); m_progressBar.SetPos(message->progress);
break; break;
case Message::UpdateFinished: case UpdateMessage::UpdateFinished:
{ {
std::string message; std::string message;
m_finishButton.EnableWindow(true); m_finishButton.EnableWindow(true);

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Platform.h" #include "Platform.h"
#include "UpdateMessage.h"
#include "UpdateObserver.h" #include "UpdateObserver.h"
#include "wincore.h" #include "wincore.h"
@ -25,26 +26,6 @@ class UpdateDialogWin32 : public UpdateObserver
LRESULT WINAPI windowProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam); LRESULT WINAPI windowProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam);
private: private:
struct Message
{
enum Type
{
UpdateFailed,
UpdateProgress,
UpdateFinished
};
Message(Type _type)
: type(_type)
, progress(0)
{
}
Type type;
std::string message;
int progress;
};
void installWindowProc(CWnd* window); void installWindowProc(CWnd* window);
CWinApp m_app; CWinApp m_app;

37
src/UpdateMessage.h Normal file
View file

@ -0,0 +1,37 @@
#pragma once
#include <string>
class UpdateMessage
{
public:
enum Type
{
UpdateFailed,
UpdateProgress,
UpdateFinished
};
UpdateMessage(void* receiver, Type type)
{
init(receiver,type);
}
UpdateMessage(Type type)
{
init(0,type);
}
void init(void* receiver, Type type)
{
this->progress = 0;
this->receiver = receiver;
this->type = type;
}
void* receiver;
Type type;
std::string message;
int progress;
};