<commit_before>﻿using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;
using System.IO;
using System.Text.RegularExpressions;


namespace ApkShellext2 {
    [Guid("dcb629fc-f86f-456f-8e24-98b9b2643a9b")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".apk")]
    public class ApkContextMenu : SharpContextMenu {
        /// <summary>
        /// Determines whether this instance can a shell context show menu, given the specified selected file list.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance should show a shell context menu for the specified file list; otherwise, <c>false</c>.
        /// </returns>
        protected override bool CanShowMenu() {
            return true;
        }

        /// <summary>
        /// Creates the context menu. This can be a single menu item or a tree of them.
        /// </summary>
        /// <returns>The context menu for the shell context menu.</returns>
        protected override ContextMenuStrip CreateMenu() {
            var menu = new ContextMenuStrip();

            var mainMenu = new ToolStripMenuItem {
                Text = "APK Shell Extension",
                Image = new Bitmap(Properties.Resources.logo, 16, 16)
            };

            var renameMenu = new ToolStripMenuItem {
                Text = "Rename with version"
            };

            renameMenu.Click += (sender, args) => renameWithVersion();

            var playMenu = new ToolStripMenuItem {
                Text = "Check GooglePlay",
                Image = new Bitmap(Properties.Resources.google_play, 16, 16)
            };

            playMenu.Click += (sender, args) => gotoGooglePlay();

            var checkUpdateMenu = new ToolStripMenuItem {
                Text = "About",
                Image = mainMenu.Image
            };

            checkUpdateMenu.Click += (sender, args) => gotoGitHub();

            mainMenu.DropDownItems.Add(renameMenu);
            if (SelectedItemPaths.Count() == 1) {
                mainMenu.DropDownItems.Add(playMenu);
            }
            mainMenu.DropDownItems.Add("-");
            mainMenu.DropDownItems.Add(checkUpdateMenu);

            menu.Items.Add(mainMenu);
            return menu;
        }

        private void renameWithVersion() {
            foreach (string path in SelectedItemPaths) {
                if (path.EndsWith(".apk", StringComparison.CurrentCultureIgnoreCase)) {
                    try {
                        ApkQuickReader reader = new ApkQuickReader(path);
                        string newfilename = reader.getAttribute("application", "label")
                             + "_" + reader.getAttribute("manifest", "versionName") + ".apk";
                        Regex rgx = new Regex(@"[\/:*?""<>|\s]");
                        if (FolderPath == null || FolderPath == "") {
                            newfilename = Path.GetDirectoryName(path) + @"\" + rgx.Replace(newfilename, "_");
                        } else {
                            newfilename = FolderPath + @"\" + rgx.Replace(newfilename, "_");
                        }

                        File.Move(path, newfilename);
                    } catch (Exception e) {<commit_msg>rename target name shows in menu<commit_after>﻿using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;
using System.IO;
using System.Text.RegularExpressions;


namespace ApkShellext2 {
    [Guid("dcb629fc-f86f-456f-8e24-98b9b2643a9b")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".apk")]
    public class ApkContextMenu : SharpContextMenu {
        /// <summary>
        /// Determines whether this instance can a shell context show menu, given the specified selected file list.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance should show a shell context menu for the specified file list; otherwise, <c>false</c>.
        /// </returns>
        protected override bool CanShowMenu() {
            return true;
        }

        /// <summary>
        /// Creates the context menu. This can be a single menu item or a tree of them.
        /// </summary>
        /// <returns>The context menu for the shell context menu.</returns>
        protected override ContextMenuStrip CreateMenu() {
            var menu = new ContextMenuStrip();

            var mainMenu = new ToolStripMenuItem {
                Text = "APK Shell Extension",
                Image = new Bitmap(Properties.Resources.logo_16x16, 16, 16)
            };

            ToolStripMenuItem renameMenu = new ToolStripMenuItem() {
                Text = "Rename as Label_Version.apk",
                Image = new Bitmap(Properties.Resources.rename_16x16, 16, 16)
            };
            if (SelectedItemPaths.Count() == 1) {
                ApkQuickReader reader = new ApkQuickReader(SelectedItemPaths.ElementAt(0));
                string newfilename = reader.getAttribute("application", "label")
                     + "_" + reader.getAttribute("manifest", "versionName") + ".apk";
                Regex rgx = new Regex(@"[\/:*?""<>|\s]");
                newfilename = rgx.Replace(newfilename, "_");
                renameMenu.Text = "Rename as " + newfilename;                
            }

            renameMenu.Click += (sender, args) => renameWithVersion();

            var playMenu = new ToolStripMenuItem {
                Text = "Check GooglePlay",
                Image = new Bitmap(Properties.Resources.google_play_16x16, 16, 16)
            };

            playMenu.Click += (sender, args) => gotoGooglePlay();            

            var checkUpdateMenu = new ToolStripMenuItem {
                Text = "ApkShellext2 on github",
                Image = new Bitmap(Properties.Resources.github_16_16,16,16)
            };

            checkUpdateMenu.Click += (sender, args) => gotoGitHub();

            mainMenu.DropDownItems.Add(renameMenu);
            if (SelectedItemPaths.Count() == 1) {
                mainMenu.DropDownItems.Add(playMenu);
            }
            mainMenu.DropDownItems.Add("-");
            mainMenu.DropDownItems.Add(checkUpdateMenu);

            menu.Items.Add(mainMenu);
            return menu;
        }

        private void renameWithVersion() {
            foreach (string path in SelectedItemPaths) {
                if (path.EndsWith(".apk", StringComparison.CurrentCultureIgnoreCase)) {
                    try {
                        ApkQuickReader reader = new ApkQuickReader(path);
                        string newfilename = reader.getAttribute("application", "label")
                             + "_" + reader.getAttribute("manifest", "versionName") + ".apk";
                        Regex rgx = new Regex(@"[\/:*?""<>|\s]");
                        if (FolderPath == null || FolderPath == "") {
                            newfilename = Path.GetDirectoryName(path) + @"\" + rgx.Replace(newfilename, "_");
                        } else {
                            newfilename = FolderPath + @"\" + rgx.Replace(newfilename, "_");
                        }

                        File.Move(path, newfilename);
                    } catch (Exception e) {
{'fraction_non_alphanumeric': 0.11149273447820343, 'fraction_numerical': 0.011228533685601057, 'mean_word_length': 1.813452248234857, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '751496', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1878, 'n_words': 461}