Example #1
0
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            FILEUTIL.SafeDelete(this.ps.TmpDirName);
            FILEUTIL.SafeDelete(this.ps.InkFileName);
#if DEBUG
#else
            FILEUTIL.SafeDelete(this.ps.FileName);
            FILEUTIL.SafeDelete(this.ps.IniFileName);
#endif
        }
Example #2
0
        public async Task <bool> Analyze()
        {
            bool ret = false;

            await Task.Run(() =>
            {
                // インク消費量からカラー/モノクロのページ数を算出
                string[] args =
                {
                    "gs",
                    "-dBATCH",
                    "-dNOPAUSE",
                    "-dSAFER",
                    "-dNoCancel",
                    "-sDEVICE=inkcov",
                    "-sOutputFile=" + InkFileName,
                    "-r75",
                    "-f",
                    FileName
                };
                int result = GSDLL.Execute(args);

                Microsoft.VisualBasic.FileIO.TextFieldParser tfp = new Microsoft.VisualBasic.FileIO.TextFieldParser(InkFileName);
                tfp.Delimiters = new string[] { " " };
                while (!tfp.EndOfData)
                {
                    string[] fields = tfp.ReadFields();
                    double[] ink    = { 0.0, 0.0, 0.0, 0.0 };
                    int i           = 0;
                    foreach (string s in fields)
                    {
                        if (s.Length > 0)
                        {
                            ink[i] = Convert.ToDouble(s);
                            i++;
                        }
                        if (i > 3)
                        {
                            break;
                        }
                    }
                    if (ink[0] + ink[1] + ink[2] > 0.0)
                    {
                        Pages.Color++;
                    }
                    else if (ink[3] > 0.0)
                    {
                        Pages.Mono++;
                    }
                    else
                    {
                        Pages.Blank++;
                    }
                }
                tfp.Close();
                ret = true;
                FILEUTIL.SafeDelete(InkFileName);
            });

            return(ret);
        }
Example #3
0
        private async void buttonPrint_Click(object sender, EventArgs e)
        {
            //bool duplexSelectable = comboDuplex.Enabled;
            //bool colorSelectable = comboColor.Enabled;

            EnableControls(false);
            string statusText = labelStatus.Text;

            var chars  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var pwd    = new char[32];
            var random = new Random();

            for (int i = 0; i < pwd.Length; i++)
            {
                pwd[i] = chars[random.Next(chars.Length)];
            }
            string jobname = new string(pwd);

            try
            {
                string printer = null;

                if (comboPrinter.SelectedIndex >= 0)
                {
                    printer = comboPrinter.SelectedItem.ToString();
                }
                else
                {
                    throw new Exception("プリンタが選択されていません");
                }

                System.Drawing.Printing.PrinterSettings pset = new System.Drawing.Printing.PrinterSettings();
                pset.PrinterName = printer;
                if (!pset.IsValid)
                {
                    throw new Exception("選択されたプリンタは有効なプリンタではありません");
                }
                bool supported_paper = false;
                for (int i = 0; i < pset.PaperSizes.Count; i++)
                {
                    if (pset.PaperSizes[i].RawKind == comboPaper.SelectedIndex)
                    {
                        supported_paper = true;
                        break;
                    }
                }
                if (!supported_paper)
                {
                    throw new Exception("選択されたプリンタはこの用紙サイズを印刷できません\n\n" +
                                        "用紙サイズ: " + comboPaper.SelectedItem.ToString());
                }

                int color_count = this.ps.Pages.Color;
                int mono_count  = this.ps.Pages.Mono;
                if (comboColor.SelectedIndex == (int)PSFile.Color.MONO)
                {
                    mono_count += color_count;
                    color_count = 0;
                }
                WebAPI.PrintInfo info = await WebAPI.Payment(textUserName.Text, textPassword.Text, color_count, mono_count, this.ps.Pages.Blank, comboPaper.SelectedIndex, comboPrinter.SelectedItem.ToString(), INIFILE.GetValue("JOBINFO", "Document", "", this.ps.IniFileName));

                if (info == null)
                {
                    throw new Exception("認証サーバに接続できませんでした。(コード:" + WebAPI.StatusCode.ToString() + ")");
                }
                if (info.result != "OK")
                {
                    throw new Exception(info.message.Replace("\\n", "\r\n"));
                }
                labelAnalysis.Text = info.message.Replace("\\n", "\r\n");
                labelAnalysis.Refresh();

                // Task t = Task.Run(() => MessageBox.Show(labelAnalysis.Text));

                REG.PrinterName = printer;
                REG.JobName     = jobname;

                labelStatus.Text = "印刷中...";
                statusStrip1.Refresh();
                try
                {
                    this.ps.Print(printer, jobname,
                                  comboPaper.SelectedIndex < 0 ? 9 : comboPaper.SelectedIndex,
                                  comboColor.SelectedIndex < 0 ? PSFile.Color.AUTO : (PSFile.Color)comboColor.SelectedIndex,
                                  comboDuplex.SelectedIndex < 0 ? PSFile.Duplex.SIMPLEX : (PSFile.Duplex)comboDuplex.SelectedIndex,
                                  INIFILE.GetValue("DEVMODE", "Collate", 1, this.ps.IniFileName) == 1,
                                  INIFILE.GetValue("DEVMODE", "Copies", 1, this.ps.IniFileName));
#if DEBUG
#else
                    FILEUTIL.SafeDelete(this.ps.FileName);
                    FILEUTIL.SafeDelete(this.ps.IniFileName);
#endif
                    await Task.Delay(1500);

                    labelAnalysis.Text = "印刷が送信されました";
                    labelAnalysis.Refresh();
                    await Task.Delay(1500);

                    this.Close();
                }
                catch (Exception ex)
                {
                    labelStatus.Text = "印刷時エラー";
                    statusStrip1.Refresh();
                    throw new Exception("印刷処理ができませんでした\r\n\r\n理由:" + ex.Message + "\r\n詳細:" + ex.HResult.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Properties.Resources.Title, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                string printer = "";
                if (comboPrinter.SelectedIndex >= 0)
                {
                    printer = comboPrinter.SelectedItem.ToString();
                }
                try
                {
                    bool b = await WebAPI.Alert(textUserName.Text, this.ps.Pages.Color, this.ps.Pages.Mono, this.ps.Pages.Blank, comboPaper.SelectedIndex, printer, INIFILE.GetValue("JOBINFO", "Document", "", this.ps.IniFileName), ex.Message);
                }
                catch
                {
#if DEBUG
                    Console.WriteLine("double exception");
#endif
                }
            }
            finally
            {
                labelAnalysis.Text = "";
                labelAnalysis.Refresh();
                EnableControls(true);
                labelStatus.Text = statusText;
            }
        }
Example #4
0
        private async void MainForm_Shown(object sender, EventArgs e)
        {
            this.Activate();
            this.TopMost = true;
            this.Activate();
            this.TopMost = false;

            EnableControls(false);

            comboPaper.SelectedIndex = INIFILE.GetValue("DEVMODE", "PaperSize", 9, this.ps.IniFileName);
            previewBox.Orientation   = INIFILE.GetValue("DEVMODE", "Orientation", 1, this.ps.IniFileName);
            string documentname = INIFILE.GetValue("JOBINFO", "Document", "", this.ps.IniFileName);

            if (documentname.Length > 0)
            {
                this.Text = Properties.Resources.Title + " - " + documentname;
            }

            if (this.ps.IsValid())
            {
                this.Refresh();
                labelStatus.Text = "ファイル解析中";
                statusStrip1.Refresh();

                /*
                 * プレビュー作成
                 */
                labelAnalysis.Text = "印刷データを読み込んでいます…";
                labelAnalysis.Refresh();
                timer1.Interval = 3000;
                timer1.Enabled  = true;
                this.Refresh();
                try
                {
                    // サムネイル作成
                    System.IO.Directory.CreateDirectory(this.ps.TmpDirName);
                    string output = System.IO.Path.Combine(ps.TmpDirName, "def-%04d.png");

                    bool b = await ps.ExportPreview1(output);
                }
                catch (ExternalException ex)
                {
                    if (ex.HResult != -100)
                    {
                        labelPreviewError.Visible = true;
                    }
                }
                catch
                {
                    labelPreviewError.Visible = true;
                }

                try
                {
                    if (System.IO.Directory.GetFiles(ps.TmpDirName, "*", System.IO.SearchOption.TopDirectoryOnly).Length > 0)
                    {
                        previewBox.FileName = System.IO.Path.Combine(ps.TmpDirName, "def-0001.png");
                    }
                    FILEUTIL.SafeDelete(ps.TmpDirName);
                }
                catch
                {
                    // do nothing
                }
                timer1.Stop();
                timer1.Enabled = false;


                /*
                 * ページ数算出
                 */
                labelAnalysis.Text = "ページ数をカウントしています";
                labelAnalysis.Refresh();
                timer2.Interval = 1000;
                timer2.Enabled  = true;
                this.Refresh();
                try
                {
                    bool b = await ps.Analyze();

                    labelAnalysis.Text = "計 " + this.ps.Pages.Total + " 枚";
                    labelAnalysis.Refresh();

                    DisplayPageAndCost();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("印刷データの解析ができませんでした\r\n\r\n理由:" + ex.Message + "\r\n詳細:" + ex.HResult.ToString(), Properties.Resources.Title);
                    labelStatus.Text = "解析エラー";
                }
                timer1.Enabled = false;
                timer2.Enabled = false;
                statusStrip1.Refresh();
                labelAnalysis.Text = "";
                this.Refresh();
            }

            this.Activate();
            this.TopMost = true;
            this.Activate();
            this.textUserName.Focus();
            this.Activate();
            this.TopMost = false;

            EnableControls(true);

            if (this.ps.Pages.Total > 0)
            {
                lock (lockObject)
                {
                    if (INIFILE.GetValue("DEVMODE", "Color", 2, this.ps.IniFileName) == 1)
                    {
                        isColorEnabled           = comboColor.Enabled = false;
                        comboColor.SelectedIndex = (int)PSFile.Color.MONO;
                    }
                    if (this.ps.Pages.Total > 1)
                    {
                        isDuplexEnabled = comboDuplex.Enabled = true;
                    }
                    else
                    {
                        comboDuplex.SelectedIndex = (int)PSFile.Duplex.SIMPLEX;
                        isDuplexEnabled           = comboDuplex.Enabled = false;
                    }
                    isPasswordEnabled = textPassword.Enabled = true;
                    isUserNameEnabled = textUserName.Enabled = true;
                    isPrintEnabled    = buttonPrint.Enabled = true;
                    textUserName.Focus();
                }
            }
        }