2019-11-26 13:19:29 +00:00
# ifndef slic3r_GUI_RemovableDriveManager_hpp_
# define slic3r_GUI_RemovableDriveManager_hpp_
# include <vector>
# include <string>
2019-12-21 11:31:32 +00:00
# include <functional>
2019-11-26 13:19:29 +00:00
namespace Slic3r {
namespace GUI {
2019-12-11 09:16:32 +00:00
# if __APPLE__
2019-12-11 10:00:47 +00:00
class RDMMMWrapper ;
# endif
2019-12-11 09:16:32 +00:00
2019-11-26 13:19:29 +00:00
struct DriveData
{
2019-11-26 14:52:18 +00:00
std : : string name ;
2019-11-26 13:19:29 +00:00
std : : string path ;
2019-11-26 14:52:18 +00:00
DriveData ( std : : string n , std : : string p ) : name ( n ) , path ( p ) { }
2019-11-26 13:19:29 +00:00
} ;
class RemovableDriveManager
{
2019-12-11 10:00:47 +00:00
# if __APPLE__
friend class RDMMMWrapper ;
# endif
2019-11-26 13:19:29 +00:00
public :
2019-11-28 12:50:58 +00:00
static RemovableDriveManager & get_instance ( )
2019-11-26 13:19:29 +00:00
{
static RemovableDriveManager instance ;
return instance ;
}
RemovableDriveManager ( RemovableDriveManager const & ) = delete ;
void operator = ( RemovableDriveManager const & ) = delete ;
2019-12-16 12:53:12 +00:00
~ RemovableDriveManager ( ) ;
2019-12-13 10:52:08 +00:00
//call only once. on apple register for unmnount callbacks. on windows register for device notification is prepared but not called (eject usb drive on widnows doesnt trigger the callback, sdc ard does), also enumerates devices for first time so init shoud be called on linux too.
2019-12-06 15:51:04 +00:00
void init ( ) ;
2019-12-13 10:52:08 +00:00
//update() searches for removable devices, returns false if empty. /time = 0 is forced update, time expects wxGetLocalTime()
bool update ( const long time = 0 , const bool check = false ) ;
2020-01-22 16:05:26 +00:00
bool is_drive_mounted ( const std : : string & path ) const ;
2019-11-28 12:50:58 +00:00
void eject_drive ( const std : : string & path ) ;
2019-12-13 10:52:08 +00:00
//returns path to last drive which was used, if none was used, returns device that was enumerated last
2020-01-22 16:05:26 +00:00
std : : string get_last_save_path ( ) const ;
std : : string get_last_save_name ( ) const ;
2019-12-13 10:52:08 +00:00
//returns path to last drive which was used, if none was used, returns empty string
2019-12-11 13:53:28 +00:00
std : : string get_drive_path ( ) ;
2020-01-22 16:05:26 +00:00
std : : vector < DriveData > get_all_drives ( ) const ;
2019-11-28 12:50:58 +00:00
bool is_path_on_removable_drive ( const std : : string & path ) ;
2019-12-13 10:52:08 +00:00
// callback will notify only if device with last save path was removed
2020-01-02 15:30:28 +00:00
void add_remove_callback ( std : : function < void ( ) > callback ) ;
2020-01-03 09:33:44 +00:00
// erases all remove callbacks added by add_remove_callback()
2019-12-13 10:52:08 +00:00
void erase_callbacks ( ) ;
2020-01-03 09:33:44 +00:00
//drive_count_changed callback is called on every added or removed device
void set_drive_count_changed_callback ( std : : function < void ( const bool ) > callback ) ;
//thi serves to set correct value for drive_count_changed callback
void set_plater_ready_to_slice ( bool b ) ;
2019-12-13 10:52:08 +00:00
// marks one of the eveices in vector as last used
2019-12-05 13:07:02 +00:00
void set_last_save_path ( const std : : string & path ) ;
2019-12-16 16:15:27 +00:00
void verify_last_save_path ( ) ;
2019-12-11 11:28:51 +00:00
bool is_last_drive_removed ( ) ;
2019-12-13 10:52:08 +00:00
// param as update()
bool is_last_drive_removed_with_update ( const long time = 0 ) ;
2019-12-13 17:02:25 +00:00
void set_is_writing ( const bool b ) ;
2020-01-22 16:05:26 +00:00
bool get_is_writing ( ) const ;
bool get_did_eject ( ) const ;
2019-12-18 09:08:17 +00:00
void set_did_eject ( const bool b ) ;
2020-01-22 16:05:26 +00:00
std : : string get_drive_name ( const std : : string & path ) const ;
size_t get_drives_count ( ) const ;
std : : string get_ejected_path ( ) const ;
std : : string get_ejected_name ( ) const ;
2019-11-26 13:19:29 +00:00
private :
2019-12-11 10:00:47 +00:00
RemovableDriveManager ( ) ;
2019-11-28 12:50:58 +00:00
void search_for_drives ( ) ;
2019-12-13 10:52:08 +00:00
//triggers callbacks if last used drive was removed
2019-11-28 12:50:58 +00:00
void check_and_notify ( ) ;
2019-12-13 10:52:08 +00:00
//returns drive path (same as path in DriveData) if exists otherwise empty string ""
std : : string get_drive_from_path ( const std : : string & path ) ;
2019-12-11 11:28:51 +00:00
void reset_last_save_path ( ) ;
2019-11-28 12:50:58 +00:00
std : : vector < DriveData > m_current_drives ;
std : : vector < std : : function < void ( ) > > m_callbacks ;
2020-01-03 09:33:44 +00:00
std : : function < void ( const bool ) > m_drive_count_changed_callback ;
2019-12-03 09:09:53 +00:00
size_t m_drives_count ;
2019-12-04 14:27:33 +00:00
long m_last_update ;
2019-12-05 13:07:02 +00:00
std : : string m_last_save_path ;
2019-12-16 16:15:27 +00:00
bool m_last_save_path_verified ;
2019-12-13 17:02:25 +00:00
std : : string m_last_save_name ;
bool m_is_writing ; //on device
2019-12-16 12:53:12 +00:00
bool m_did_eject ;
2020-01-03 09:33:44 +00:00
bool m_plater_ready_to_slice ;
2020-01-21 09:23:50 +00:00
std : : string m_ejected_path ;
std : : string m_ejected_name ;
2019-11-26 14:52:18 +00:00
# if _WIN32
2019-12-13 10:52:08 +00:00
//registers for notifications by creating invisible window
2019-11-28 12:50:58 +00:00
void register_window ( ) ;
2019-12-10 13:10:47 +00:00
# else
# if __APPLE__
2019-12-11 09:16:32 +00:00
RDMMMWrapper * m_rdmmm ;
2019-12-10 13:10:47 +00:00
# endif
2019-12-10 10:17:12 +00:00
void search_path ( const std : : string & path , const std : : string & parent_path ) ;
bool compare_filesystem_id ( const std : : string & path_a , const std : : string & path_b ) ;
2019-12-11 10:00:47 +00:00
void inspect_file ( const std : : string & path , const std : : string & parent_path ) ;
2019-11-26 14:52:18 +00:00
# endif
2019-11-26 13:19:29 +00:00
} ;
2019-12-13 10:52:08 +00:00
// apple wrapper for RemovableDriveManagerMM which searches for drives and/or ejects them
2019-12-11 10:00:47 +00:00
# if __APPLE__
class RDMMMWrapper
{
public :
RDMMMWrapper ( ) ;
~ RDMMMWrapper ( ) ;
void register_window ( ) ;
void list_devices ( ) ;
2019-12-17 12:08:17 +00:00
void eject_device ( const std : : string & path ) ;
2019-12-11 15:59:26 +00:00
void log ( const std : : string & msg ) ;
2019-12-11 10:00:47 +00:00
protected :
void * m_imp ;
//friend void RemovableDriveManager::inspect_file(const std::string &path, const std::string &parent_path);
} ;
2019-12-10 13:10:47 +00:00
# endif
2019-11-26 13:19:29 +00:00
} }
2019-12-10 10:17:12 +00:00
# endif
2019-12-13 14:26:42 +00:00