📄 filetype.c
字号:
/*
filetype.c -- Handling connection files
20011009 (Mark Melvin)
*/
#include <stdlib.h>
#include <windows.h>
#include <shellapi.h>
#include <commdlg.h>
#include <string.h>
#include <io.h>
#ifndef WIN32
#include <dir.h>
#include <dirent.h>
#endif
#include <stdio.h>
#include "utils.h"
#include "dtelnet.h"
#include "emul.h"
#include "socket.h"
#include "connect.h"
#include "raw.h"
#include "dialog.h"
#include "filetype.h"
#include "dtchars.h"
/* Section header inside the session file */
#define SECTION "Telnet Shortcut"
#define FILETYPE ".ts"
#define FILETYPEEXT "ts"
#define EXTLEN 3
#define DESCRIPTION "DTelnet Shortcut"
/* TYPE_REG_LEN is the length of TYPE_REG, these two must be kept in sync */
#define TYPE_REG "DTelnetShortcut"
#define TYPE_REG_LEN 15
static HWND manageDlg = NULL; /* manage dialog handle */
static BOOL haveEditDlg = FALSE;
static Connect editVars;
static char dlgTitle[_MAX_PATH];
static BOOL newFile;
static char saveName[_MAX_PATH];
extern struct Protocols protocols[];
extern unsigned int numProtocols;
/* fill the path of favorites file */
void expandFavPath(char *fullname, char *name)
{
getExePath(telnetGetInstance(), fullname);
strlcat(fullname, name, _MAX_PATH);
strlcat(fullname, FILETYPE, _MAX_PATH);
}
/* Load a shortcut file into Connect structure */
BOOL fileLoadSession (char* filename, Connect* conn)
{
char buf[10];
char fullpath[_MAX_PATH];
char tmp[512];
if (!strchr(filename, '\\')) {
expandFavPath(fullpath, filename);
}
else {
strlcpy(fullpath, filename, _MAX_PATH);
}
if (access(fullpath,0)!=0)
{
char message[_MAX_PATH];
sprintf(message, "Error opening file %s ", fullpath);
MessageBox(NULL, message, "DTelnet", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
/* Load variables from file with ini-file format */
GetPrivateProfileString(SECTION, "Host", "", conn->host, HOST_NAME_LEN, fullpath);
GetPrivateProfileString(SECTION, "Port", "", conn->port, PORT_NAME_LEN, fullpath);
GetPrivateProfileString(SECTION, "Protocol", "none", conn->protocol, PROTOCOL_NAME_LEN, fullpath);
GetPrivateProfileString(SECTION, "Term", "", conn->term, TERM_NAME_LEN, fullpath);
GetPrivateProfileString(SECTION, "User", "", conn->user, USER_NAME_LEN, fullpath);
GetPrivateProfileString(SECTION, "Charset", "", conn->charset, CHARSET_NAME_LEN, fullpath);
GetPrivateProfileString(SECTION, "BS2Del", "", buf, 9, fullpath);
if (strnicmp(buf, "TRUE", 9)==0)
conn->bs2Del = TRUE;
else
conn->bs2Del = FALSE;
GetPrivateProfileString(SECTION, "AnswerBack", "*NOTSET", tmp, sizeof (tmp),
fullpath);
if (strcmp (tmp, "*NOTSET")!=0) {
conn->abacklen =
(short)unescape (tmp, strlen (tmp),
conn->answerback, sizeof (conn->answerback));
} /* else keep original setting */
return TRUE;
}
/* Save current session's parameter */
static void fileSaveSession(const Connect *conn, char* filename)
/* filename is the full path */
{
char str[20];
WritePrivateProfileString(SECTION, "Host", conn->host, filename);
WritePrivateProfileString(SECTION, "Port", conn->port, filename);
WritePrivateProfileString(SECTION, "Protocol", conn->protocol, filename);
WritePrivateProfileString(SECTION, "Term", conn->term, filename);
WritePrivateProfileString(SECTION, "User", conn->user, filename);
WritePrivateProfileString(SECTION, "Charset", conn->charset, filename);
strcpy(str, conn->bs2Del?"TRUE":"FALSE");
WritePrivateProfileString(SECTION, "BS2Del", str, filename);
}
/* Associate our file type with dtelnet */
void fileTypeRegister(void)
{
HKEY hkDtelnet;
char exePath[_MAX_PATH];
char value[_MAX_PATH];
GetModuleFileName(telnetGetInstance(), exePath, _MAX_PATH);
if (RegCreateKey(HKEY_CLASSES_ROOT, TYPE_REG, &hkDtelnet)
== ERROR_SUCCESS)
{
#ifdef WIN32
/* Win32-specific keys, we can set description and icons. */
RegSetValue(HKEY_CLASSES_ROOT, TYPE_REG, REG_SZ, "DTelnet Shortcut", 16);
strcpy(value, exePath);
strlcat(value, ",1", _MAX_PATH);
RegSetValue(hkDtelnet, "DefaultIcon", REG_SZ, value, strlen(value));
#endif
#ifdef WIN32
/* Insert double-quote marks to handle names like "Program Files" */
if (strlen(exePath)+2 < _MAX_PATH)
{
sprintf(value, "\"%s\"", exePath);
}
#else
strcpy(value, exePath);
#endif
strlcat(value, " /X \"%1\"", _MAX_PATH);
RegSetValue(hkDtelnet, "shell\\open\\command", REG_SZ, value, strlen(value));
RegCloseKey(hkDtelnet);
/* Link the extension to the file type */
RegSetValue(HKEY_CLASSES_ROOT, FILETYPE, REG_SZ, TYPE_REG, TYPE_REG_LEN);
#ifdef WIN32
/* Notify the system that it's been updated */
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0);
#endif
}
}
/* Compare filename to our extension */
BOOL fileCheckType(char* filename)
{
char* extpart;
/* pointer arithmetic, should take us to right before extension part */
extpart = filename + (strlen(filename)-EXTLEN);
if (stricmp(extpart, FILETYPE)==0)
{
extpart[0] = 0; //truncate extension
return TRUE;
}
return FALSE;
}
/* enable delete/modify buttons if the listbox have selection
*/
static void fileManageEnableCtls(void)
{
BOOL bEnable;
bEnable = (SendDlgItemMessage(manageDlg, ID_FAV_LISTBOX, LB_GETCURSEL, 0, 0) != LB_ERR);
EnableWindow(GetDlgItem(manageDlg, ID_FAV_DEL), bEnable);
EnableWindow(GetDlgItem(manageDlg, ID_FAV_MOD), bEnable);
if (!bEnable) {
SendDlgItemMessage(manageDlg, ID_FAV_INFO, WM_SETTEXT, 0, (LPARAM)"No selection");
}
}
/*
* flag FILETYPE_LOAD_MENU update favorites menu
* flag FILETYPE_LOAD_MANAGE update manage favorites listbox
*/
void fileLoadFavorites(UINT flags)
{
char *name;
char dtelnetdir[_MAX_PATH];
HWND listbox = GetDlgItem(manageDlg, ID_FAV_LISTBOX);
HMENU favmenu = GetSubMenu(GetMenu(telnetGetWnd()),2);
#ifndef WIN32
DIR *dir;
struct dirent *ent;
#else
HANDLE hfind;
WIN32_FIND_DATA wfd;
#endif
int item = ID_FAVORITES_FIRST;
getExePath(telnetGetInstance(), dtelnetdir);
if (flags & FILETYPE_LOAD_MENU) {
/* Clear the menu fisrt */
while (GetMenuItemCount(favmenu) > 3){
DeleteMenu(favmenu, 3, MF_BYPOSITION);
}
}
if (flags & FILETYPE_LOAD_MANAGE) {
/* Clear the listbox */
SendMessage(listbox, LB_RESETCONTENT, 0, 0);
fileManageEnableCtls();
}
#ifndef WIN32
dir = opendir(dtelnetdir);
if (!dir) {
return;
}
while (((ent=readdir(dir)) != NULL)&&(item < ID_FAVORITES_MAX))
{
name = ent->d_name;
#else
strlcat(dtelnetdir, "*"FILETYPE, _MAX_PATH);
hfind = FindFirstFile(dtelnetdir, &wfd);
if (hfind == INVALID_HANDLE_VALUE) {
return;
}
do {
name = wfd.cFileName;
#endif
if (fileCheckType(name))
{
if (flags & FILETYPE_LOAD_MENU) {
AppendMenu(favmenu, MF_STRING, item, name);
item++;
}
if (flags & FILETYPE_LOAD_MANAGE) {
SendMessage(listbox, LB_ADDSTRING, 0, (LPARAM) name);
}
}
#ifndef WIN32
}
closedir(dir);
#else
} while (FindNextFile(hfind, &wfd)&&(item < ID_FAVORITES_MAX));
FindClose(hfind);
#endif
if (flags & FILETYPE_LOAD_MENU) {
DrawMenuBar(telnetGetWnd());
}
}
void fileExecFavorites(HMENU favmenu, WPARAM item)
{
char name[_MAX_PATH];
GetMenuString(favmenu, item, name, sizeof(name), MF_BYCOMMAND);
if (!socketConnected())
{
/* Not connected, we load the session in current window */
Connect conn;
connectGetDefVars (&conn);
if (fileLoadSession(name, &conn))
{
connectLoadVars(&conn);
connectOpenHost();
}
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -