Browse Source

Backported API changes from other projects, no activity because busy with other projects

master
Sasko Usinov 3 months ago
parent
commit
5a8064d71b
7 changed files with 286 additions and 220 deletions
  1. +220
    -205
      application.h
  2. +1
    -1
      dm4l.pro.user
  3. +4
    -1
      lib/libsources/api.h
  4. +3
    -3
      lib/libsources/fs.cpp
  5. +49
    -5
      lib/libsources/str.cpp
  6. +2
    -1
      lib/libsources/vct.cpp
  7. +7
    -4
      ui/main_window/main_window.h

+ 220
- 205
application.h View File

@@ -44,224 +44,239 @@
//------------------------------------------------------------------------------------------------//
// enums
//------------------------------------------------------------------------------------------------//
enum err_codes
{
OP_FAIL,
OP_SUCCESS,
OP_NO_ACTION_TAKEN
};

enum action_types
{
ACT_DISABLE_DEVICE,
ACT_ENABLE_DEVICE
};

enum scsi_action_types
{
ACTION_SCSI_NONE,
ACTION_SCSI_DISABLE_DEVICE,
ACTION_SCSI_RESCAN_DEVICES
};

enum script_types
{
SCRIPT_TYPE_PCI,
SCRIPT_TYPE_SCSI,
SCRIPT_TYPE_USB,
};
enum err_codes
{
OP_FAIL,
OP_SUCCESS,
OP_NO_ACTION_TAKEN
};

//------------------------------------------------------------------------------------------------//
// structs
//------------------------------------------------------------------------------------------------//
struct pci_device_struct
{
inline pci_device_struct() :
device_enabled(true), data(nullptr) {}

std::string kernel_module;
std::string sysfs_id;
std::string device_type;
std::string device_desc;
std::string dev_type_id;
std::string dev_desc_vid_did;
std::string boot_script_path;
std::string parent_ref;

bool device_enabled;

std::vector<std::string> prop;
void * data;
};

struct boot_entry_struct
{
std::string boot_script_path;
std::string device_desc;
std::string entry_line;
};

struct scsi_device_struct
{
std::string scsi_idx;
std::string hba_number;
std::string product_info;
std::string product_desc;
std::vector<std::string> props;
std::string boot_script_path;
};

struct usb_device_struct
{
inline usb_device_struct () :
hwi_prop_start_idx(1), device_enabled(false) {}

std::string bus_idx;
std::string device_idx;
std::string vid;
std::string did;
std::string vid_did;
std::string vid_did_processed;
std::string device_type;
std::string hub_info;
std::string actual_dev_details;
long hwi_prop_start_idx;
bool device_enabled;
std::string title;
std::string boot_script_path;
std::string sysfs_id;
};

struct error_structure
{
public:
inline error_structure () : err_code(EXIT_SUCCESS) {}

inline void set_error (std::string msg, int code)
{err_message = msg; err_code = code; if (msg.length()) api::writerr.println(msg);}
inline std::string get_error_str () {return err_message;}
inline int get_error_code () {return err_code;}

private:
std::string err_message;
int err_code;
};

struct file_watcher_data
{
size_t size;
signed long idx;
signed long length;
int file_descriptor;
int watch_descriptor;
char buffer[WATCHER_BUFF_SIZE];
std::string dirpath;

inline file_watcher_data (const std::string & path = "/dev") :
size (WATCHER_BUFF_SIZE),
idx (0),
length (0),
file_descriptor (0),
watch_descriptor (0),
buffer (),
dirpath(path) {}
};
enum action_types
{
ACT_DISABLE_DEVICE,
ACT_ENABLE_DEVICE
};

enum scsi_action_types
{
ACTION_SCSI_NONE,
ACTION_SCSI_DISABLE_DEVICE,
ACTION_SCSI_RESCAN_DEVICES
};

enum script_types
{
SCRIPT_TYPE_PCI,
SCRIPT_TYPE_SCSI,
SCRIPT_TYPE_USB,
};
//-------------------------------------------------------------------------------//
// end enums
//-------------------------------------------------------------------------------//

//------------------------------------------------------------------------------------------------//
// namespaces
// structs
//------------------------------------------------------------------------------------------------//
namespace app
{
//------------------------------------------------------------------------------------------------//
// externs
//------------------------------------------------------------------------------------------------//
extern std::vector<pci_device_struct> pci_list;
extern std::vector<boot_entry_struct> boot_list;
extern std::vector<scsi_device_struct> scsi_list;
extern std::vector<usb_device_struct> usb_list;

extern std::vector<pci_device_struct> pci_search_list;
extern std::vector<scsi_device_struct> scsi_search_list;
extern std::vector<usb_device_struct> usb_search_list;

extern error_structure last_err;

//------------------------------------------------------------------------------------------------//
// sigc
//------------------------------------------------------------------------------------------------//
extern sigc::signal <void> sigc_clear_device_list;
extern sigc::signal <void, std::vector<pci_device_struct> & > sigc_load_pci_list;
extern sigc::signal <void, std::vector<scsi_device_struct> &> sigc_load_scsi_list;
extern sigc::signal <void, std::vector<usb_device_struct> &> sigc_load_usb_list;

//------------------------------------------------------------------------------------------------//
// we need to keep an eye on the connection and disconnect to prevent freezes
//------------------------------------------------------------------------------------------------//

extern sigc::signal <void, const std::string &> sigc_file_added;
extern sigc::signal <void, const std::string &> sigc_file_removed;
extern sigc::signal <void, const std::string &> sigc_dev_query_end;

extern sigc::connection sigc_conn_file_added;
extern sigc::connection sigc_conn_file_removed;

//------------------------------------------------------------------------------------------------//
// funtions and classes
//------------------------------------------------------------------------------------------------//
void load_pci_device_list ();
void load_scsi_device_list ();
void load_usb_device_list ();

std::string query_device (const std::string & dev_node);

void search_for_device (const char * dev_str);

std::vector<std::string> get_device_prop (const std::string & sysfs_id);

void t_device_monitor (const std::string & path);

int config_env ();
void print_config ();
struct pci_device_struct
{
inline pci_device_struct() :
device_enabled(true), data(nullptr) {}

inline bool can_access (const std::string & app) {return !system(std::string("bash -c \"" + SHELL_PATH + " && which " + app + " > /dev/null\"").c_str());}
std::string kernel_module;
std::string sysfs_id;
std::string device_type;
std::string device_desc;
std::string dev_type_id;
std::string dev_desc_vid_did;
std::string boot_script_path;
std::string parent_ref;

void init_watch_list ();
bool device_enabled;

void show_notification (const std::string & title, const std::string & msg, int delay);
std::vector<std::string> prop;
void * data;
};

inline bool lshw_found () {return can_access("lshw");}
inline bool hwinfo_found () {return can_access("hwinfo");}
inline bool lsusb_found () {return can_access("lsusb");}
inline bool lspci_found () {return can_access("lspci");}
struct boot_entry_struct
{
std::string boot_script_path;
std::string device_desc;
std::string entry_line;
};

namespace about
struct scsi_device_struct
{
const std::string app_name {"dm4l"};
const std::string app_friendly_name {TXT_WINDOW_TITLE};
const std::string app_desc {TXT_TAG_LINE};
const std::string app_version {"0.01"};
const std::string app_build_date {__DATE__};
const std::string app_build_time {__TIME__};
const std::string app_author {"Sasko Usinov"};
const std::string app_contact {"neolinux.tech@optusnet.com.au"};
const std::string app_src {"https://gitlab.com/neolinux.tech/device-manager-4-linux"};
const std::string app_website {"neolinux.tech" };
}
std::string scsi_idx;
std::string hba_number;
std::string product_info;
std::string product_desc;
std::vector<std::string> props;
std::string boot_script_path;
};

struct usb_device_struct
{
inline usb_device_struct () :
hwi_prop_start_idx(1), device_enabled(false) {}

std::string bus_idx;
std::string device_idx;
std::string vid;
std::string did;
std::string vid_did;
std::string vid_did_processed;
std::string device_type;
std::string hub_info;
std::string actual_dev_details;
long hwi_prop_start_idx;
bool device_enabled;
std::string title;
std::string boot_script_path;
std::string sysfs_id;
};

//-------------------------------------------------------------------------------//
// Used for error values
//-------------------------------------------------------------------------------//
struct error_structure
{
public:
inline error_structure () : err_code(EXIT_SUCCESS) {}

inline void set_error (std::string msg, int code)
{err_message = msg; err_code = code; if (msg.length()) api::writerr.println(msg);}
inline std::string get_error_str () {return err_message;}
inline int get_error_code () {return err_code;}

private:
std::string err_message;
int err_code;
};

//-------------------------------------------------------------------------------//
// Used for file monitoring
//-------------------------------------------------------------------------------//
struct file_watcher_data
{
size_t size;
signed long idx;
signed long length;
int file_descriptor;
int watch_descriptor;
char buffer[WATCHER_BUFF_SIZE];
std::string dirpath;

inline file_watcher_data (const std::string & path = "/dev") :
size (WATCHER_BUFF_SIZE),
idx (0),
length (0),
file_descriptor (0),
watch_descriptor (0),
buffer (),
dirpath(path) {}
};
//-------------------------------------------------------------------------------//
// end structs
//-------------------------------------------------------------------------------//

namespace data

//------------------------------------------------------------------------------------------------//
// namespaces
//------------------------------------------------------------------------------------------------//
namespace app
{
extern std::string app_log_dir;
extern std::string app_home_dir;
extern std::string app_settings_dir;
extern std::string app_scripts_dir;
extern std::string app_backup_dir;

extern file_watcher_data * fwd;
extern bool exiting;
extern std::vector<std::string> watch_list;
extern std::vector<std::thread> thread_list;
//------------------------------------------------------------------------------------------------//
// externs
//------------------------------------------------------------------------------------------------//
extern std::vector<pci_device_struct> pci_list;
extern std::vector<boot_entry_struct> boot_list;
extern std::vector<scsi_device_struct> scsi_list;
extern std::vector<usb_device_struct> usb_list;

extern std::vector<pci_device_struct> pci_search_list;
extern std::vector<scsi_device_struct> scsi_search_list;
extern std::vector<usb_device_struct> usb_search_list;

extern error_structure last_err;

//------------------------------------------------------------------------------------------------//
// sigc
//------------------------------------------------------------------------------------------------//
extern sigc::signal <void> sigc_clear_device_list;
extern sigc::signal <void, std::vector<pci_device_struct> & > sigc_load_pci_list;
extern sigc::signal <void, std::vector<scsi_device_struct> &> sigc_load_scsi_list;
extern sigc::signal <void, std::vector<usb_device_struct> &> sigc_load_usb_list;

//------------------------------------------------------------------------------------------------//
// we need to keep an eye on the connection and disconnect to prevent freezes
//------------------------------------------------------------------------------------------------//

extern sigc::signal <void, const std::string &> sigc_file_added;
extern sigc::signal <void, const std::string &> sigc_file_removed;
extern sigc::signal <void, const std::string &> sigc_dev_query_end;

extern sigc::connection sigc_conn_file_added;
extern sigc::connection sigc_conn_file_removed;

//------------------------------------------------------------------------------------------------//
// funtions and classes
//------------------------------------------------------------------------------------------------//
void load_pci_device_list ();
void load_scsi_device_list ();
void load_usb_device_list ();

std::string query_device (const std::string & dev_node);

void search_for_device (const char * dev_str);

std::vector<std::string> get_device_prop (const std::string & sysfs_id);

void t_device_monitor (const std::string & path);

int config_env ();
void print_config ();

inline bool can_access (const std::string & app) {return !system(std::string("bash -c \"" + SHELL_PATH + " && which " + app + " > /dev/null\"").c_str());}

void init_watch_list ();

void show_notification (const std::string & title, const std::string & msg, int delay);

inline bool lshw_found () {return can_access("lshw");}
inline bool hwinfo_found () {return can_access("hwinfo");}
inline bool lsusb_found () {return can_access("lsusb");}
inline bool lspci_found () {return can_access("lspci");}

namespace about
{
const std::string app_name {"dm4l"};
const std::string app_friendly_name {TXT_WINDOW_TITLE};
const std::string app_desc {TXT_TAG_LINE};
const std::string app_version {"0.01"};
const std::string app_build_date {__DATE__};
const std::string app_build_time {__TIME__};
const std::string app_author {"Sasko Usinov"};
const std::string app_contact {"neolinux.tech@optusnet.com.au"};
const std::string app_src {"https://gitlab.com/neolinux.tech/device-manager-4-linux"};
const std::string app_website {"neolinux.tech" };
}

namespace data
{
extern std::string app_log_dir;
extern std::string app_home_dir;
extern std::string app_settings_dir;
extern std::string app_scripts_dir;
extern std::string app_backup_dir;

extern file_watcher_data * fwd;
extern bool exiting;
extern std::vector<std::string> watch_list;
extern std::vector<std::thread> thread_list;
}
}
}
//-------------------------------------------------------------------------------//
// namespaces end
//-------------------------------------------------------------------------------//

#endif

+ 1
- 1
dm4l.pro.user View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.9.1, 2020-01-19T21:31:12. -->
<!-- Written by QtCreator 4.9.1, 2020-02-08T19:37:04. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>


+ 4
- 1
lib/libsources/api.h View File

@@ -44,6 +44,7 @@
namespace api
{
#define API_ERROR_IDX_NOT_FOUND std::string::npos
#define API_ERROR_INVALID_IDX -1

#define API_ERROR_FILE_WRITE 1
#define API_ERROR_FILE_OPEN 2
@@ -84,6 +85,8 @@
std::vector<std::string> url2list (const std::string & url);
std::string enclose_string (const std::string & enc, const std::string & src);
bool check_for_special_chars (const std::string & src);
std::string get_file_extension_from_path (const std::string & path_name);
std::string remove_file_extension (const std::string & path_name);
}

namespace fs
@@ -92,7 +95,7 @@

std::string generate_unique_filename (const std::string & path, const std::string & prefix, int start_idx = 0);
bool path_exists (const std::string &file_path);
bool save_to_file (const std::string & file_path, const std::string & content);
bool save_to_file (const std::string & file_path, const std::string & content, bool append = false);
std::string load_from_file (const std::string & file_path);
bool is_directory (const std::string & file_path);



+ 3
- 3
lib/libsources/fs.cpp View File

@@ -92,11 +92,11 @@ bool api::fs::path_exists(const std::string & file_path)
// Returns: true on success otherwise false
//
//------------------------------------------------------------------------------//
bool api::fs::save_to_file(const std::string & file_path, const std::string & content)
bool api::fs::save_to_file(const std::string & file_path, const std::string & content, bool append)
{
try
{
std::ofstream file_stream (file_path.c_str());
std::ofstream file_stream (file_path.c_str(), append ? std::ofstream::out | std::ofstream::app : std::ofstream::out);

if (!file_stream.is_open())
return false;
@@ -107,7 +107,7 @@ bool api::fs::save_to_file(const std::string & file_path, const std::string & co
}
catch (std::exception & e)
{
api::writerr << NAME_TO_STRING(API_ERROR_FILE_WRITE) << " when opening " << file_path << " for reading. Error is: " << e.what() << api::endl;
api::writerr << NAME_TO_STRING(API_ERROR_FILE_WRITE) << " when opening " << file_path << ". Error is: " << e.what() << api::endl;
return false;
}



+ 49
- 5
lib/libsources/str.cpp View File

@@ -57,7 +57,7 @@
//-------------------------------------------------------------------------------//
size_t api::str::get_last_index_of(const char & ch, const std::string & src)
{
size_t out {std::string::npos};
size_t out {API_ERROR_IDX_NOT_FOUND};

for (size_t i = 0; i < src.length(); i++)
(src[i] == ch) ? out = i : out;
@@ -99,7 +99,7 @@ std::string api::str::remove_char(const char & ch, const std::string & src)
//------------------------------------------------------------------------------//
std::string api::str::load_from_file(const std::string & local_file)
{
std::ifstream ifs(local_file.c_str());
std::ifstream ifs (local_file.c_str());

if (!ifs.is_open()) return std::string();

@@ -294,7 +294,7 @@ std::string api::str::to_upper_case(std::string & str)
// Type: namespace
//-------------------------------------------------------------------------------//
//
// ToDo: Rewrite me...old code
// ToDo: ...
//
// Notes: Returns the name of the file only if path_name contained an absolute or relative path
//
@@ -322,7 +322,7 @@ std::string api::str::get_file_name_from_url(const std::string & path_name, cons
//-------------------------------------------------------------------------------//
size_t api::str::get_char_index(const char & ch, const size_t & count, const std::string & src, size_t pos_from)
{
size_t out {std::string::npos};
size_t out {API_ERROR_IDX_NOT_FOUND};
size_t counter {0};

for (size_t idx = pos_from; idx < src.length(); idx++)
@@ -336,7 +336,7 @@ size_t api::str::get_char_index(const char & ch, const size_t & count, const std
if (counter == count) return out;
}

return std::string::npos;
return API_ERROR_IDX_NOT_FOUND;
}

//-------------------------------------------------------------------------------//
@@ -520,4 +520,48 @@ bool api::str::check_for_special_chars(const std::string & src)
return false;
}

//-------------------------------------------------------------------------------//
// Type: namespace
//-------------------------------------------------------------------------------//
//
// ToDo: n/a
//
// Notes: Returns the file extension contained in path_name
//
// Returns: See description
//
//-------------------------------------------------------------------------------//
std::string api::str::get_file_extension_from_path(const std::string & path_name)
{
size_t dot_pos(api::str::get_last_index_of('.', path_name));

if (dot_pos == 0) return path_name;
if (dot_pos == path_name.length() - 1) return path_name;

if (dot_pos != API_ERROR_IDX_NOT_FOUND)
return api::str::range_copy(dot_pos + 1, path_name.length() - 1, path_name);

return path_name;
}


//-------------------------------------------------------------------------------//
// Type: namespace
//-------------------------------------------------------------------------------//
//
// ToDo: n/a
//
// Notes: Removes the file extenstion from path_name
//
// Returns: See desciption
//
//-------------------------------------------------------------------------------//
std::string api::str::remove_file_extension(const std::string & path_name)
{
std::size_t last_dot = api::str::get_last_index_of('.', path_name);

if (last_dot == 0) return path_name;
if (last_dot == std::string::npos) return path_name;

return api::str::range_copy(0, last_dot - 1, path_name);
}

+ 2
- 1
lib/libsources/vct.cpp View File

@@ -140,7 +140,8 @@ std::vector<std::string> api::vct::to_vector_string_list(const std::string & src
//
// Notes: Returns the index of str in list if str exists in list
//
// Returns: The position index of str in list or -1 if it does not exist
// Returns: The position index of str in list or API_ERROR_IDX_NOT_FOUND if
// it does not exist
//
// To Do: n/a
//


+ 7
- 4
ui/main_window/main_window.h View File

@@ -135,6 +135,9 @@ class main_window : public QDialog
explicit main_window (QWidget *parent = 0);
inline ~ main_window () {delete ui;}

//-------------------------------------------------------------------------------//
// accessors
//-------------------------------------------------------------------------------//
inline QPushButton * get_corner_button () {return corner_button;}
inline QTabWidget * get_main_pager () {return ui->u_mw_tbw_main;}

@@ -165,7 +168,7 @@ class main_window : public QDialog
void register_meta_types ();

//------------------------------------------------------------------------------------------------//
// class methods
// general internal use
//------------------------------------------------------------------------------------------------//
void configure_gui ();
void setup_default_geometry ();
@@ -217,7 +220,7 @@ class main_window : public QDialog
void detach_file_mon ();

//------------------------------------------------------------------------------------------------//
// This one is threaded
// threaded...
//------------------------------------------------------------------------------------------------//
void t_run_shell_script (const std::string & script_path, int script_type);
void t_show_device_info (const std::string & file_path);
@@ -305,9 +308,9 @@ class main_window : public QDialog

inline void slotq_on_read_rc_local_clicked () {read_rc_local(); new toast_message(this, "Done!", 4000, 500);}

void slotq_corner_button_clicked ();
void slotq_corner_button_clicked ();

void slotq_device_prop_clicked ();
void slotq_device_prop_clicked ();
inline void slotq_on_usb_device_prop_clicked () {show_usb_device_prop();}

inline void slotq_on_about_qt_clicked () {qApp->aboutQt();}


Loading…
Cancel
Save