📄 efax_controller.cpp
字号:
if (!prog_config.lock_file.empty()) unlink(prog_config.lock_file.c_str()); bool restart_standby = false; bool end_receive = false; switch(state) { case EfaxController::sending: case EfaxController::send_on_standby: if (!exit_code) { save_sent_fax(); if (last_fax_item_sent.is_socket_file) { // if we are sending a print job via the socket server, notify the // Socket_server object which created the temporary file (the server // object will clean up by deleting it and also cause the socket // file list to be updated - we do not need to do that here) remove_from_socket_server_filelist(last_fax_item_sent.file_list[0]); } } else cleanup_fax_send_fail(); unjoin_child(); { int state_val = state; state = inactive; if (state_val == send_on_standby && !close_down) { receive(receive_standby); } // we do not need to call display_state() if we have called receive(), as // receive() will call display_state() itself else display_state(); } break; case EfaxController::start_send_on_standby: clear_receive_dirname(); unjoin_child(); if (!close_down) sendfax(last_fax_item_sent); else { state = inactive; display_state(); } break; case EfaxController::receive_standby: if ((!exit_code || exit_code == 3) && !close_down) { restart_standby = true; } else end_receive = true; break; default: if (!inactive) end_receive = true; break; } if (end_receive || restart_standby) { clear_receive_dirname(); unjoin_child(); state = inactive; // this is needed even if we are going to call receive() // now restart if in standby mode if (restart_standby) receive(receive_standby); // we do not need to call display_state() if we have called receive(), as // receive() will call display_state() itself else display_state(); restart_standby = false; end_receive = false; } if (close_down && state == inactive) ready_to_quit_notify(); } /* uncomment for debugging else { std::cout << "Not efax_controller child" << std::endl; std::cout << "Child pid is " << result << std::endl; std::cout << "Child exit code is " << exit_code << std::endl; } */ }}void EfaxController::clear_receive_dirname(void) { if (*prog_config.receive_dirname) { // now delete receive_dirname if it is empty std::string full_dirname; if (!prog_config.working_dir.empty()) { full_dirname = prog_config.working_dir; full_dirname += "/faxin/"; } full_dirname += prog_config.receive_dirname; int result = rmdir(full_dirname.c_str()); // only deletes the directory if it is empty if (result == -1 && errno == ENOTEMPTY) { // before assuming a fax has been successfully received, check to see // if [fax_name].001 is of 0 size (if it is delete it) struct stat statinfo; std::string file_name(full_dirname); file_name += '/'; file_name += prog_config.receive_dirname; file_name += ".001"; if (!stat(file_name.c_str(), &statinfo) && !statinfo.st_size) { unlink(file_name.c_str()); rmdir(full_dirname.c_str()); } else fax_received_notify(); } // lock the Prog_config object while we modify it Glib::Mutex::Lock lock(*prog_config.mutex_p); *prog_config.receive_dirname = 0; }}void EfaxController::join_child(void) { // we do not need a mutex for this - join_child() and unjoin_child() are // only called in the initial (GUI) thread stdout_connection = Glib::signal_io().connect(sigc::mem_fun(*this, &EfaxController::read_pipe_slot), stdout_pipe.get_read_fd(), Glib::IO_IN);}void EfaxController::unjoin_child(void) { // we do not need a mutex for this - join_child() and unjoin_child() are // only called in the initial (GUI) thread, and stdout_pipe is only manipulated // in that thread or after a new single-threaded process has been launched with fork() stdout_pipe.close(); stdout_connection.disconnect();}void EfaxController::kill_child(void) { if (child_pid) kill(child_pid, SIGTERM); Glib::usleep(500000); // now really make sure if (child_pid) kill(child_pid, SIGKILL);}void EfaxController::save_sent_fax(void) { // get a time value to create the directory name into which the fax is to be saved struct std::tm* time_p; std::time_t time_count; std::time(&time_count); time_p = std::localtime(&time_count); // now create the directory into which the fax files to be saved std::string fileout_name; if (!prog_config.working_dir.empty()) { fileout_name = prog_config.working_dir; fileout_name += "/faxsent/"; } else { write_error("You don't have the $HOME environmental variable set.\n" "You may need to search for the directory in which\n" "the fax is saved, and it probably won't appear in\n" "the fax list!\n"); } const char dirname_format[] = "%y%m%d%H%M%S"; char top_dirname[MAX_TEMP_NAME + 1]; std::strftime(top_dirname, MAX_TEMP_NAME + 1, dirname_format, time_p); std::string temp(fileout_name); temp += top_dirname; // check whether directory already exists or can't be created int count; for (count = 0; count < 4 && mkdir(temp.c_str(), S_IRUSR | S_IWUSR | S_IXUSR); count++) { sleep(1); // wait a second to get a different time std::time(&time_count); time_p = std::localtime(&time_count); std::strftime(top_dirname, MAX_TEMP_NAME + 1, dirname_format, time_p); temp = fileout_name + top_dirname; } if (count == 4) { write_error("Can't create directory to save fax\n"); return; } // now make fileout_name the same as the directory name for the saved faxes // that we have just created fileout_name += top_dirname; const std::string fileout_dirname(fileout_name); // keep for later to enter a description // and now complete the unsuffixed file name of the destination files fileout_name += '/'; fileout_name += top_dirname; // now create a string containing the date for the fax description // glibc has const struct tm* as last param of strftime() // but the const does not appear to be guaranteed by POSIX // so do localtime() again just in case time_p = std::localtime(&time_count); const char date_description_format[] = "(%H%M %Z %d %b %Y)"; const int max_description_datesize = 126; char date_description[max_description_datesize]; std::strftime(date_description, max_description_datesize, date_description_format, time_p); // now copy files into the relevant directory for the fax pages to be saved std::vector<std::string> file_basename_vec; // this is used in making the fax description int out_partnumber = 1; // in order to generate the suffix for each fax page as saved std::vector<std::string>::const_iterator sentname_iter; for (sentname_iter = last_fax_item_sent.file_list.begin(); sentname_iter != last_fax_item_sent.file_list.end(); ++sentname_iter) { std::string::size_type pos = sentname_iter->find_last_of('/'); if (pos == std::string::npos || pos + 2 > sentname_iter->size()) { write_error("Not valid file name to save -- can't save sent fax\n"); } else { // save the file base name for later use in making the fax description pos++; file_basename_vec.push_back(sentname_iter->substr(pos)); // make the suffixes int in_partnumber = 1;#ifdef HAVE_STRINGSTREAM std::ostringstream in_suffix_strm; in_suffix_strm << '.' << std::setfill('0') << std::setw(3) << in_partnumber; std::ostringstream out_suffix_strm; out_suffix_strm << '.' << std::setfill('0') << std::setw(3) << out_partnumber; // make the suffixed source and destination files std::string suffixed_inname = *sentname_iter + in_suffix_strm.str(); std::string suffixed_outname = fileout_name + out_suffix_strm.str();#else std::ostrstream in_suffix_strm; in_suffix_strm << '.' << std::setfill('0') << std::setw(3) << in_partnumber << std::ends; const char* in_suffix = in_suffix_strm.str(); std::ostrstream out_suffix_strm; out_suffix_strm << '.' << std::setfill('0') << std::setw(3) << out_partnumber << std::ends; const char* out_suffix = out_suffix_strm.str(); // make the suffixed source and destination files std::string suffixed_inname = *sentname_iter + in_suffix; std::string suffixed_outname = fileout_name + out_suffix; delete[] in_suffix; delete[] out_suffix;#endif std::ifstream filein; std::ofstream fileout; const int BLOCKSIZE = 1024; char block[BLOCKSIZE]; while (!access(suffixed_inname.c_str(), R_OK) && (filein.open(suffixed_inname.c_str(), std::ios::in), filein) // use comma operator to check filein && (fileout.open(suffixed_outname.c_str(), std::ios::out), fileout)) { // ditto for fileout while (filein) { filein.read(block, BLOCKSIZE); fileout.write(block, filein.gcount()); } filein.close(); filein.clear(); fileout.close(); fileout.clear(); unlink(suffixed_inname.c_str()); in_partnumber++; out_partnumber++;#ifdef HAVE_STRINGSTREAM in_suffix_strm.str(""); in_suffix_strm << '.' << std::setfill('0') << std::setw(3) << in_partnumber; out_suffix_strm.str(""); out_suffix_strm << '.' << std::setfill('0') << std::setw(3) << out_partnumber; // make the suffixed source and destination files suffixed_inname = *sentname_iter + in_suffix_strm.str(); suffixed_outname = fileout_name + out_suffix_strm.str();#else std::ostrstream in_suffix_strm; in_suffix_strm << '.' << std::setfill('0') << std::setw(3) << in_partnumber << std::ends; in_suffix = in_suffix_strm.str(); std::ostrstream out_suffix_strm; out_suffix_strm << '.' << std::setfill('0') << std::setw(3) << out_partnumber << std::ends; out_suffix = out_suffix_strm.str(); // make the suffixed source and destination files suffixed_inname = *sentname_iter + in_suffix; suffixed_outname = fileout_name + out_suffix; delete[] in_suffix; delete[] out_suffix;#endif } fileout.clear(); if (in_partnumber < 2) write_error("There was a problem saving all or part of the sent fax\n"); } } // now save the sent fax description if (out_partnumber > 1) { const std::string description_filename(fileout_dirname + "/Description"); std::ofstream fileout(description_filename.c_str(), std::ios::out); if (fileout) { if (last_fax_item_sent.is_socket_file) fileout << gettext("PRINT JOB"); else { std::vector<std::string>::const_iterator filename_iter; for (filename_iter = file_basename_vec.begin(); filename_iter != file_basename_vec.end(); ++filename_iter) { if (filename_iter != file_basename_vec.begin()) fileout << '+'; fileout << *filename_iter; } } if (!last_fax_item_sent.number.empty()) { fileout << " --> " << last_fax_item_sent.number; } fileout << ' ' << date_description; } else write_error("Cannot save sent fax description\n"); }}void EfaxController::cleanup_fax_send_fail(void) { std::vector<std::string>::const_iterator filename_iter; for (filename_iter = last_fax_item_sent.file_list.begin(); filename_iter != last_fax_item_sent.file_list.end(); ++filename_iter) { // make the suffix int partnumber = 1;#ifdef HAVE_STRINGSTREAM std::ostringstream strm; strm << '.' << std::setfill('0') << std::setw(3) << partnumber; // make the suffixed file name std::string filename = *filename_iter + strm.str(); while (!access(filename.c_str(), R_OK)) { unlink(filename.c_str()); partnumber++; strm.str(""); strm << '.' << std::setfill('0') << std::setw(3) << partnumber; filename = *filename_iter + strm.str(); }#else std::ostrstream strm; strm << '.' << std::setfill('0') << std::setw(3) << partnumber << std::ends; const char* suffix = strm.str(); // make the suffixed file name std::string filename = *filename_iter + suffix; delete[] suffix; while (!access(filename.c_str(), R_OK)) { unlink(filename.c_str()); partnumber++; std::ostrstream strm; strm << '.' << std::setfill('0') << std::setw(3) << partnumber << std::ends; suffix = strm.str(); filename = *filename_iter + suffix; delete[] suffix; }#endif }}bool EfaxController::is_receiving_fax(void) const { bool return_val = false; if (*prog_config.receive_dirname) { std::string file_name(prog_config.working_dir); file_name += "/faxin/"; file_name += prog_config.receive_dirname; file_name += '/'; file_name += prog_config.receive_dirname; file_name += ".001"; struct stat statinfo; if (!stat(file_name.c_str(), &statinfo)) return_val = true; } return return_val;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -