2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2011-2016, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
8 #include "GraphicalUserInterface.h"
11 #include <AutoDeleter.h>
13 #include <FilePanel.h>
16 #include "GuiTeamUiSettings.h"
17 #include "MessageCodes.h"
18 #include "TeamWindow.h"
22 // #pragma mark - GraphicalUserInterface::FilePanelHandler
25 class GraphicalUserInterface::FilePanelHandler
: public BHandler
{
28 virtual ~FilePanelHandler();
32 virtual void MessageReceived(BMessage
* message
);
34 status_t
WaitForPanel();
36 void SetCurrentRef(entry_ref
* ref
);
39 { return fPanelLock
; }
42 entry_ref
* fCurrentRef
;
48 GraphicalUserInterface::FilePanelHandler::FilePanelHandler()
50 BHandler("GuiPanelHandler"),
58 GraphicalUserInterface::FilePanelHandler::~FilePanelHandler()
60 if (fPanelWaitSem
>= 0)
61 delete_sem(fPanelWaitSem
);
66 GraphicalUserInterface::FilePanelHandler::Init()
68 fPanelWaitSem
= create_sem(0, "FilePanelWaitSem");
70 if (fPanelWaitSem
< 0)
78 GraphicalUserInterface::FilePanelHandler::MessageReceived(BMessage
* message
)
80 switch (message
->what
) {
81 case MSG_USER_INTERFACE_FILE_CHOSEN
:
84 if (message
->FindRef("refs", &ref
) == B_OK
85 && fCurrentRef
!= NULL
) {
94 release_sem(fPanelWaitSem
);
99 BHandler::MessageReceived(message
);
106 GraphicalUserInterface::FilePanelHandler::WaitForPanel()
108 status_t result
= B_OK
;
110 result
= acquire_sem(fPanelWaitSem
);
111 } while (result
== B_INTERRUPTED
);
118 GraphicalUserInterface::FilePanelHandler::SetCurrentRef(entry_ref
* ref
)
124 // #pragma mark - GraphicalUserInterface
127 GraphicalUserInterface::GraphicalUserInterface()
130 fTeamWindowMessenger(NULL
),
131 fFilePanelHandler(NULL
),
137 GraphicalUserInterface::~GraphicalUserInterface()
139 delete fTeamWindowMessenger
;
145 GraphicalUserInterface::ID() const
147 return "GraphicalUserInterface";
152 GraphicalUserInterface::Init(Team
* team
, UserInterfaceListener
* listener
)
155 fTeamWindow
= TeamWindow::Create(team
, listener
);
156 fTeamWindowMessenger
= new BMessenger(fTeamWindow
);
157 fFilePanelHandler
= new FilePanelHandler();
158 status_t error
= fFilePanelHandler
->Init();
160 ERROR("Error: Failed to create file panel semaphore!\n");
163 fTeamWindow
->AddHandler(fFilePanelHandler
);
165 // start the message loop
169 // TODO: Notify the user!
170 ERROR("Error: Failed to create team window!\n");
179 GraphicalUserInterface::Show()
181 if (fTeamWindow
->IsHidden())
184 fTeamWindow
->Activate();
189 GraphicalUserInterface::Terminate()
192 if (fTeamWindowMessenger
&& fTeamWindowMessenger
->LockTarget())
198 GraphicalUserInterface::Clone() const
200 return new(std::nothrow
) GraphicalUserInterface
;
205 GraphicalUserInterface::IsInteractive() const
212 GraphicalUserInterface::LoadSettings(const TeamUiSettings
* settings
)
214 status_t result
= fTeamWindow
->LoadSettings((GuiTeamUiSettings
*)settings
);
221 GraphicalUserInterface::SaveSettings(TeamUiSettings
*& settings
) const
223 settings
= new(std::nothrow
) GuiTeamUiSettings(ID());
224 if (settings
== NULL
)
227 fTeamWindow
->SaveSettings((GuiTeamUiSettings
*)settings
);
234 GraphicalUserInterface::NotifyUser(const char* title
, const char* message
,
235 user_notification_type type
)
237 // convert notification type to alert type
238 alert_type alertType
;
240 case USER_NOTIFICATION_INFO
:
241 alertType
= B_INFO_ALERT
;
243 case USER_NOTIFICATION_WARNING
:
244 case USER_NOTIFICATION_ERROR
:
246 alertType
= B_WARNING_ALERT
;
250 BAlert
* alert
= new(std::nothrow
) BAlert(title
, message
, "OK",
251 NULL
, NULL
, B_WIDTH_AS_USUAL
, alertType
);
255 // TODO: We need to let the alert run asynchronously, but we shouldn't just
256 // create it and don't care anymore. Maybe an error window, which can
257 // display a list of errors would be the better choice.
262 GraphicalUserInterface::NotifyBackgroundWorkStatus(const char* message
)
264 fTeamWindow
->DisplayBackgroundStatus(message
);
269 GraphicalUserInterface::SynchronouslyAskUser(const char* title
,
270 const char* message
, const char* choice1
, const char* choice2
,
273 BAlert
* alert
= new(std::nothrow
) BAlert(title
, message
,
274 choice1
, choice2
, choice3
);
282 GraphicalUserInterface::SynchronouslyAskUserForFile(entry_ref
* _ref
)
284 BAutolock
lock(&fFilePanelHandler
->Locker());
286 if (fFilePanel
== NULL
) {
287 BMessenger
messenger(fFilePanelHandler
);
288 BMessage
* message
= new(std::nothrow
) BMessage(
289 MSG_USER_INTERFACE_FILE_CHOSEN
);
292 ObjectDeleter
<BMessage
> messageDeleter(message
);
293 fFilePanel
= new(std::nothrow
) BFilePanel(B_OPEN_PANEL
,
294 &messenger
, NULL
, B_FILE_NODE
, false, message
);
295 if (fFilePanel
== NULL
)
297 messageDeleter
.Detach();
300 fFilePanelHandler
->SetCurrentRef(_ref
);
302 return fFilePanelHandler
->WaitForPanel();